Skip to content

Instantly share code, notes, and snippets.

@dchentech
Created July 23, 2014 10:57
Show Gist options
  • Save dchentech/be4d8c2f73e75658cc68 to your computer and use it in GitHub Desktop.
Save dchentech/be4d8c2f73e75658cc68 to your computer and use it in GitHub Desktop.
从Singleton实现看Python里的__new__和__init__区别。
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