Skip to content

Instantly share code, notes, and snippets.

@geekKeen
Last active July 25, 2017 11:21
Show Gist options
  • Save geekKeen/ac482b761af2ca987191634609ef3cd1 to your computer and use it in GitHub Desktop.
Save geekKeen/ac482b761af2ca987191634609ef3cd1 to your computer and use it in GitHub Desktop.
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>
@geekKeen
Copy link
Author

  1. Python的工厂模式
  2. 注意 __new__ 的使用,第一个参数等于 cls 时, 返回的实例可以初始化, 如果是 cls 子类时, 不经过 __init__ 初始化

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment