Last active
July 25, 2017 11:21
-
-
Save geekKeen/ac482b761af2ca987191634609ef3cd1 to your computer and use it in GitHub Desktop.
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
class Object(object): | |
def __new__(cls, *args, **kwargs): | |
base = kwargs.pop('base') | |
instance = super(Object, cls).__new__(base) | |
instance.initalize(*args, **kwargs) | |
return instance | |
class A(Object): | |
def initalize(self, *args, **kwargs): | |
... | |
if __name__ == "__main__": | |
a = Object(base=A) | |
print type(a) # <class A> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
__new__
的使用,第一个参数等于cls
时, 返回的实例可以初始化, 如果是cls
子类时, 不经过__init__
初始化