Python 辨异 —— __init__ 与 __new__
2016-12-08 10:28
375 查看
__init__ 更多的作用是初始化属性,__new__ 进行的是创建对象,显然 __new__ 要早于 __init__ 发生。
考虑一个继承自 tuple 的类,显然在 __init__ 无法对其成员进行修改;
显然这里定义的类 Edge 可以用来抽象一些二元不可变的对象。
考虑一个继承自 tuple 的类,显然在 __init__ 无法对其成员进行修改;
class Edge(tuple): def __new__(cls, e1, e2): return tuple.__new__(cls, (e1, e2)) def __repr__(self): return 'Edge (%s, %s)' % (repr(self[0]), repr(self[1])) __str__ = __repr__
显然这里定义的类 Edge 可以用来抽象一些二元不可变的对象。
相关文章推荐
- Python中__new__() 和 __init__()
- Python中的__new__和__init__
- Python中__init__和__new__的区别详解
- 浅谈 Python 中的 __init__ 和 __new__
- python中__new__和__init__的区别
- python __new__ __init__
- python实现单例模式__init__ __new__区别
- python中的__init__ 、__new__、__call__小结
- Python中的__new__与__init__魔术方法理解笔记
- Python中__new__和__init__区别
- 详解Python中的__init__和__new__
- (转)Python中的__init__和__new__
- Python中__init__和__new__的区别详解
- python 中的 __new__ 和 __init__
- 【python】__new__和__init__区别
- Python __new__, __init__, __metaclass__, __call__, __del__, __getattr__, __getattribute__
- Python_中__init__和__new__的异同
- python __init__ __new__ __call__
- python中的__init__ 、__new__、__call__小结及使用
- python中的__init__ 、__new__、__call__小结