Python之 __slots__ 的用法
来源:互联网 发布:手机视频录播软件 编辑:程序博客网 时间:2024/06/07 13:55
使用__slots__限定class实例 能添加的属性
__slots__仅仅对当前的类实例起作用,对继承的子类不起作用。
知识点:
1、捕获异常最好使用traceback。
2、MethodType作用是将方法绑定到类上,并不是将方法直接写在类中,而是在内存中创建一个link指向外部的方法,在创建实例时候link也会得到复制。
import tracebackfrom types import MethodTypeclass MyClass(object): __slots__ = ['name','set_name']def set_name(self,name): self.name = namecls = MyClass()cls.name = 'Tom'cls.set_name = MethodType(set_name,cls)cls.set_name('Jerry')print(cls.name)try: cls.age = 50except AttributeError: traceback.print_exc()class ExtMyClass(MyClass): passext_cls = ExtMyClass()ext_cls.age = 30print(ext_cls.age)
阅读全文
0 0
- Python之 __slots__ 的用法
- Python3之__slots__用法
- python的__slots__
- python的__slots__
- Python的__slots__
- Python的__slots__
- Python __slots__的作用
- python的__slots__
- Python基础篇之__slots__
- python 类的__slots__属性
- python __slots__
- python __slots__
- Python __slots__
- Python---__slots__
- python __slots__
- [Python]新式类的__slots__属性
- python的动态性以及 使用__slots__
- python学习-面向对象进阶之__slots__(五)
- C++ 读文件示例代码
- Ubuntu下中文输入法的解决办法
- 【Leetcode】【python】Swap Nodes in Pairs
- Spring-AOP @AspectJ切点函数之@within()和@target
- C#--继承的关键字
- Python之 __slots__ 的用法
- Android底部菜单-FragmentTabHost实现
- nohup介绍
- ZeroBrane Studio 设置
- 【LeetCode】Partition List 链表划分
- Python之 @property 用法
- 关于jquery里面prop和attr的带示例详细对比,深度理解
- 文件系统学习1 四巨头
- 【LeetCode】Reverse Linked List