Python 辨异 —— __init__ 与 __new__
生活随笔
收集整理的這篇文章主要介紹了
Python 辨异 —— __init__ 与 __new__
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
__init__ 更多的作用是初始化屬性,__new__ 進行的是創建對象,顯然 __new__ 要早于 __init__ 發生。
考慮一個繼承自 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 可以用來抽象一些二元不可變的對象。
轉載于:https://www.cnblogs.com/mtcnn/p/9422538.html
總結
以上是生活随笔為你收集整理的Python 辨异 —— __init__ 与 __new__的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python连接SQL Server取多
- 下一篇: android使用bintray发布aa