上一页 1 ··· 26 27 28 29 30 31 32 33 34 ··· 39 下一页
摘要: 一切的类都是通过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 = 阅读全文
posted @ 2020-03-16 21:05 竹石2020 阅读(88) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2020-03-16 15:40 竹石2020 阅读(130) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2020-03-16 15:29 竹石2020 阅读(180) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2020-03-16 12:19 竹石2020 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2020-03-16 10:32 竹石2020 阅读(150) 评论(0) 推荐(0) 编辑
摘要: 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__) 阅读全文
posted @ 2020-03-13 21:48 竹石2020 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2020-03-13 10:44 竹石2020 阅读(180) 评论(0) 推荐(0) 编辑
摘要: with open as f: 等同于 f = obj.__enter__() 代码块 with obj >触发obj.__enter__(),拿到返回值 as f >f = 返回值 执行代码块: 一:没有异常的情况下,整个代码块运行完毕后去触发__excit__,它的三个参数都为None 二:有异 阅读全文
posted @ 2020-03-11 17:21 竹石2020 阅读(186) 评论(0) 推荐(0) 编辑
摘要: #自省、反射 hasattr(obj,'属性') # 判断obj.属性是否存在 getattr(obj,'属性') 获取obj.属性 不存在则报错 getattr(obj,'属性','默认值') 获取obj.属性的值,不存在则返回默认值 setattr(obj,'属性','属性的值') 设置属性ob 阅读全文
posted @ 2020-03-10 11:58 竹石2020 阅读(90) 评论(0) 推荐(0) 编辑
摘要: 描述符的本质是一个新式类,在这个新式类中,至少实现了__get__()、__set__()、__delete__()中的一个,这也被称为描述符协议 __get__() :调用一个属性时,触发 __set__() :为一个属性赋值时,触发 __delete__() :采用del删除属性时,触发 数据描 阅读全文
posted @ 2020-03-09 23:41 竹石2020 阅读(138) 评论(0) 推荐(0) 编辑
上一页 1 ··· 26 27 28 29 30 31 32 33 34 ··· 39 下一页