python学习-62 类属性的增删该查

类属性

1.类属性

类属性又称为静态变量,或者是静态数据。这些数据是与它们所属的类对象绑定的,不依赖于任何类实例。

2.增 删 改 查

class zoo:
    country = ‘china‘
    def __init__(self,name,address,kind):
        self.name = name
        self.address = address
        self.kind = kind
    def monkey(self):
        print(‘this is monkey (%s)‘ %self.address)
    def tiger(self):
        print(‘this is tiger (%s)‘ %self.kind)
    def end(self):
        print(‘welcome to %s‘ %self.name)
dwy = zoo(‘chinese\‘s zoo‘,‘beijing‘,‘others‘)

zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy)

# 对类的数据属性增 删 改 查

print(zoo.country)                  #  查看信息

zoo.country = ‘US‘                # 修改信息
print(zoo.country)

zoo.snake = ‘this is snake‘          # 增加
print(dwy.snake)

del zoo.country                 # 删除
del zoo.snake

print(zoo.__dict__)

运行结果:

this is monkey (beijing)
this is tiger (others)
welcome to chinese‘s zoo
china
US
this is snake
{‘__module__‘: ‘__main__‘, ‘__init__‘: <function zoo.__init__ at 0x7fb30efb8d90>, ‘monkey‘: <function zoo.monkey at 0x7fb30efb8f28>, ‘tiger‘: <function zoo.tiger at 0x7fb30efc9048>, ‘end‘: <function zoo.end at 0x7fb30efc9378>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘zoo‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘zoo‘ objects>, ‘__doc__‘: None}

Process finished with exit code 0

3.

class zoo:
    country = ‘china‘
    def __init__(self,name,address,kind):
        self.name = name
        self.address = address
        self.kind = kind
    def monkey(self):
        print(‘this is monkey (%s)‘ %self.address)
    def tiger(self):
        print(‘this is tiger (%s)‘ %self.kind)
    def end(self):
        print(‘welcome to %s‘ %self.name)
dwy = zoo(‘chinese\‘s zoo‘,‘beijing‘,‘others‘)

# 调用
zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy)

# 类的函数属性的增加

def eat(self,food):
    print(‘%s 正在吃%s‘%(self.name,food))
zoo.eat = eat
print(zoo.__dict__)                   # 以列表的方式 查看是否添加进去了

dwy.eat(‘草‘)

运行结果:

this is monkey (beijing)
this is tiger (others)
welcome to chinese‘s zoo
{‘__module__‘: ‘__main__‘, ‘country‘: ‘china‘, ‘__init__‘: <function zoo.__init__ at 0x7f90cdc3ed90>, ‘monkey‘: <function zoo.monkey at 0x7f90cdc3ef28>, ‘tiger‘: <function zoo.tiger at 0x7f90cdc4f048>, ‘end‘: <function zoo.end at 0x7f90cdc4f378>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘zoo‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘zoo‘ objects>, ‘__doc__‘: None, ‘eat‘: <function eat at 0x7f90cdd49268>}
chinese‘s zoo 正在吃草

Process finished with exit code 0

原文地址:https://www.cnblogs.com/liujinjing521/p/11457902.html

时间: 2024-08-04 09:19:25

python学习-62 类属性的增删该查的相关文章

Python学习(类属性与实例属性)

编程语言都围绕着实例(对象),函数关系,及作用域展开(对象的生命周期) Python的import sys或者其它类似于c语言编译时的链接过程,不在同一个文件中的函数关系,我们怎样才能利用到它 Python的类跟c++的类基本是同一个东西,Python没有protected的权限,只有private和public,感觉友好很多 实例属性与c++的普通成员相同,类属性则类似于静态成员(static变量),初始化一次,通过类名访问和修改,所有的实例维护类的同一份的static成员变量 __init_

Python学习之__slots__属性

在廖老师的网站上学习的__slots__属性 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017/5/14 17:16 # @Author : Aries # @Site : # @File : 使用_slots_.py # @Software: PyCharm from types import MethodType # # class Student(object): # pass # s=Student() # s.na

python学习笔记-类的descriptor

descriptor应用背景 所谓描述器,是实现了描述符协议,即get, set, 和 delete方法的对象. 简单说,描述符就是可以重复使用的属性. 比如以下代码: f = Foo() b = f.bar f.bar = c del f.bar 在解释器执行上述代码时,当发现你试图访问属性(b = f.bar).对属性赋值(f.bar = c)或者删除一个实例变量的属性(del f.bar)时,就会去调用自定义的方法. 为什么把对函数的调用伪装成对属性的访问?有什么好处? 从property

python 面向对象六 类属性和实例属性

一.实例属性 Python是动态语言,根据类创建的实例可以任意绑定属性. 1 >>> class Student(object): 2 ... def __init__(self, name): 3 ... self.name = name # 每个实例必须的属性 4 ... 5 >>> s = Student('Jack') 6 >>> s.score = 90 # 任意绑定属性 7 >>> s.name 8 'Jack' 9 &g

python学习19类5之多态与鸭子模型

'''''''''一.多态1.Python中多态是指一类事物有多种形态.''' class Animal: def run(self): raise AttributeError('子类必须实现这个方法') #抛出异常 class People(Animal): def run(self): print('人正在走') class Pig(Animal): def run(self): print('pig is walking') class Dog(Animal): def run(self

python学习之 类class

定义 Python 的 Class 比较特别,和我们习惯的静态语言类型定义有很大区别. 1. 使用一个名为 __init__ 的方法来完成初始化.2. 使用一个名为 __del__ 的方法来完成类似析购操作.3. 所有的实例方法都拥有一个 self 参数来传递当前实例,类似于 this.4. 可以使用 __class__ 来访问类型成员 >>>>>> class MyClass: def __init__(self): print "initialize.&q

Python学习_11_类和实例

类和实例 类是对象创建实例的模板,而实例则是对象的实体.类使用class关键字定义: class MyClass:? ? pass python中创建实例直接使用工厂函数(类名加上一对括号),和其他的语言使用new关键字有所不同: my_obj = MyClass() 一般来说,类名以大写字母开头,而对象名则以小写字母或者下划线开头. 实例化对象时,会执行类定义中的__init__()方法,该方法执行时包含实例的各种初始化操作. 方法和函数的区别:方法由对象调用,在方法定义中,第一个参数必须是显

python学习——定制类

看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方法我们也知道是为了能让class作用于len()函数. 除此之外,Python的class中还有许多这样有特殊用途的函数,可以帮助我们定制类. __str__ 我们先定义一个Student类,打印一个实例: >>> class Student(object): ... def __init__(self, name):

python学习13类2之封装

'''''''''面向对象三大特性:封装,继承,多态1.封装: 类中以_或者__的属性,都是私有属性,禁止外部调用.'''class Student(object): def __init__(self,name,age,sex): self.__name = name self.__age = age self.__sex = sex one = Student('wsx',18,'男') print(one._Student__name)print(one._Student__age)pri