Created
July 23, 2014 10:57
-
-
Save dchentech/be4d8c2f73e75658cc68 to your computer and use it in GitHub Desktop.
从Singleton实现看Python里的__new__和__init__区别。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from singleton import singleton | |
@singleton(True) | |
class hh(object): | |
cc = 0 | |
def __init__(self): | |
hh.cc += 1 | |
self.count = hh.cc | |
def __repr__(self): | |
return "<" + str(self.count) + ">" + str(id(self)) | |
print hh() # <1>4299409040 | |
print hh() # <2>4299409040 | |
""" | |
从Singleton实现看Python里的__new__和__init__区别。 | |
1. __new__ 是初始化类对象,比如返回类的一个实例 instance1。 | |
2. __init__ 是对初始化后的 instance1 的内部属性进行初始化,所以它的返回在 Python 里规定是空。 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment