摘要:
一切的类都是通过type这个类来创建的,例如: 1 class Foo: 2 a = 1 3 pass 4 5 6 print(type(Foo)) 7 print(Foo.__dict__) 8 9 10 def __init__(self, name, age): 11 self.name = 阅读全文
摘要:
1 class Goods: 2 def __init__(self,price,discount): 3 self.price = price 4 self.discount = discount 5 6 @property 7 def price1(self): 8 return self.pr 阅读全文
摘要:
1 class property2: 2 def __init__(self, func): 3 print('执行property2') 4 self.func = func 5 6 def __get__(self, instance, owner): 7 print('执行get') 8 se 阅读全文
摘要:
1 class property2: 2 def __init__(self, func): 3 print('执行property2') 4 self.func = func 5 6 def __get__(self, instance, owner): 7 return self.func(in 阅读全文
摘要:
1 class Type: 2 def __init__(self, key, expcet_type): 3 self.key = key 4 self.expct_type = expcet_type 5 6 def __get__(self, instance, owner): 7 print 阅读全文
摘要:
1 def demo(obj): 2 print(' ') 3 obj.x = 1 4 obj.y = 2 5 obj.z = 3 6 return obj 7 8 @demo 9 class Foo: 10 pass 11 12 f1 = Foo() 13 print(Foo.__dict__) 阅读全文
摘要:
1 class Type: 2 def __init__(self,key): 3 self.key = key 4 5 def __get__(self, instance, owner): 6 print('执行get方法') 7 return instance.__dict__[self.ke 阅读全文
摘要:
with open as f: 等同于 f = obj.__enter__() 代码块 with obj >触发obj.__enter__(),拿到返回值 as f >f = 返回值 执行代码块: 一:没有异常的情况下,整个代码块运行完毕后去触发__excit__,它的三个参数都为None 二:有异 阅读全文
摘要:
#自省、反射 hasattr(obj,'属性') # 判断obj.属性是否存在 getattr(obj,'属性') 获取obj.属性 不存在则报错 getattr(obj,'属性','默认值') 获取obj.属性的值,不存在则返回默认值 setattr(obj,'属性','属性的值') 设置属性ob 阅读全文
摘要:
描述符的本质是一个新式类,在这个新式类中,至少实现了__get__()、__set__()、__delete__()中的一个,这也被称为描述符协议 __get__() :调用一个属性时,触发 __set__() :为一个属性赋值时,触发 __delete__() :采用del删除属性时,触发 数据描 阅读全文