Python 类属性和方法

import types

class Dog(object):

    __slots__ = ("name", "color", "info")
#定义__slots__
#该类中只允许 类对象 动态增加 name, color, info, body_len 属性或方法(都是实例属性或方法)
#__slots__对类的 动态添加属性和方法 没有限制,而 类对象 不能再动态添加对象属性和方法
#__slots__只对类对象进行限制,不对类进行限制
#_slots__仅对当前类起作用,对继承的子类不起作用
#__slots__不仅限制类对象的属性,还限制类对象的方法
#在子类中定义__slots__,子类允许定义的属性就是自身的__slots__加上父类的__slots__ company = '蓝月公司' #company 是类属性
len_attr = 1 #初始化狗的长度 def __init__(self, name):
self.name = name
#name 为实例属性 @staticmethod #静态方法可以写在类内
def state():
print("Dog 类的静态方法(state)") @classmethod #类方法可以写在类内
def get_state(cls):
print("狗狗的产地是" + cls.address) @property
def body_len(self):
return self.len_attr #如果不存在 __slots__ 作为约束,并且类属性不存在 len_attr 则 property 相当于新定义了一个实例属性 len_attr
#方法名(body_len)不能与其设置的属性名(len_attr)相同 @body_len.setter #装饰器名(body_len.setter)的前半部分必须与上文被装饰器 property 装饰的函数(body_len)同名
def body_len(self, value): #此处的方法名必须与上文中的被装饰器装饰的方法名相同,如果不同也不会报错,
#因为得到值本质调用的是上文被装饰器property装饰的函数(函数名并不重要)
#而设置值本质调用的是被装饰器 body_len.setter 装饰的函数(函数名并不重要)
#但为了能实现 把方法"变成"属性 ,这里的函数名必须与上文的函数名同名,否则就失去了装饰器 @property 存在的意义
if value > 2:
print("狗的体长必须小于2米")
else:
Dog.len_attr = value def get_name(self):
return self.name def set_name(self, name):
self.name = name name_attr = property(fget=get_name, fset=set_name, fdel=None, doc="设置狗的名字")
#property 函数就相当于 @property 装饰器 d = Dog('萨摩耶') #实例化 Dog 对象
e = Dog('土狗') d.color = 'black' #只给 d 对象增加 color 属性,Dog 基类和其它 Dog 对象并未有 color 属性 print(hasattr(d, 'color')) #判断 d 对象是否具有 color属性
print(hasattr(e, 'color')) Dog.address = '中国' #给 Dog 基类增加 address 属性 print(hasattr(d, 'address'))
print(hasattr(e, 'address')) def info(self):
print("产于" + self.address + "的" +self.name + "的颜色为" + self.color) d.info = types.MethodType(info, d) #只给 d 对象增加 info 方法,Dog 基类和其它 Dog 对象并未有 info 方法
d.info() #此方法为实例方法 print(hasattr(d, 'info'))
print(hasattr(e, 'info')) @staticmethod #静态方法可以写在类外(但必须指定(#000000#))
def show():
print("Dog 类的静态方法(show)") Dog.show = show #000000#
Dog.show()
d.show()
e.show()
d.state()
e.state()
Dog.state() @classmethod
def get_show(cls): #cls 只能调用 类属性 而不能调用 实例属性
print("公司:" + cls.company + " 产地:" + cls.address)
print(hasattr(cls, 'name')) #不存在 name 属性,因为它是 实例属性 Dog.get_show = get_show
d.get_show() d.body_len = 3
d.body_len = 1.8
print("当前长度" + str(d.body_len) + "米") print(d.name_attr)
d.name_attr = "杜宾犬"
print(d.name_attr)

Python 类属性和方法

上一篇:Unicode 和 UTF-8 有何区别


下一篇:tensorflow2.0--fashion