摘要: 1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 import contextlib 4 5 6 @contextlib.contextmanager 7 def file_open(file_name): 8 print('file_open') 9 yield 10 print('file e... 阅读全文
posted @ 2019-07-24 17:17 _simpleSmile 阅读(171) 评论(0) 推荐(0) 编辑
摘要: 1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 4 # 上下文管理器协议 5 6 7 class Sample: 8 def __enter__(self): 9 print('enter') 10 return self 11 12 def __exit__(sel... 阅读全文
posted @ 2019-07-24 17:09 _simpleSmile 阅读(189) 评论(0) 推荐(0) 编辑
摘要: 可以看到,上面的Airplane类实现了多继承,不过它继承的第二个类我们起名为PlaneMixin,而不是Plane,这个并不影响功能,但是会告诉后来读代码的人,这个类是一个Mixin类。所以从含义上理解,Airplane只是一个Vehicle,不是一个Plane。这个Mixin,表示混入(mix- 阅读全文
posted @ 2019-07-24 16:48 _simpleSmile 阅读(132) 评论(0) 推荐(0) 编辑
摘要: (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>) DBCA super其实是根据mro算法来调用的 阅读全文
posted @ 2019-07-24 16:32 _simpleSmile 阅读(250) 评论(0) 推荐(0) 编辑
摘要: • Python中比较常见的自省(introspection)机制(函数用法)有: dir(),type(), hasattr(), isinstance(),通过这些函数,我们能够在程序运行时得知对象的类型,判断对象是否存在某个属性,访问对象的属性。 dir() dir() 函数可能是 Pytho 阅读全文
posted @ 2019-07-24 16:04 _simpleSmile 阅读(422) 评论(0) 推荐(0) 编辑
摘要: • 在Python中,实例属性如果以双下划线开头,那么这个属性就是一个私有属性 但是,Python实现这种私有属性的方法,仅仅是通过改变该变量的名称来达到的 __x --> _Test__x 阅读全文
posted @ 2019-07-24 15:52 _simpleSmile 阅读(235) 评论(0) 推荐(0) 编辑
摘要: • 有@classmetod装饰器的是类方法 • 有@staticmetod装饰器的是静态方法 • 在形参列表里面必须加入self参数的一般是实例方法 阅读全文
posted @ 2019-07-24 15:16 _simpleSmile 阅读(126) 评论(0) 推荐(0) 编辑