Python中__new__和__init__区别
2013-10-14 14:08
447 查看
__new__:创建对象时调用,会返回当前对象的一个实例
__init__:创建完对象后调用,对当前对象的一些实例初始化,无返回值
1、在类中,如果__new__和__init__同时存在,会优先调用__new__
2、__new__方法会返回所构造的对象,__init__则不会。__init__无返回值。
If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().
如果__new__返回一个对象的实例,会隐式调用__init__
If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.
如果__new__不返回一个对象的实例,__init__不会被调用
object.__init__(self[, ...])
Called when the instance is created. The arguments are those passed to the class constructor expression. If a base class has an __init__() method, the derived class’s __init__() method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: BaseClass.__init__(self, [args...]). As a special constraint on constructors, no value may be returned; doing so will cause a TypeError to be raised at runtime.
在对象的实例创建完成后调用。参数被传给类的构造函数。如果基类有__init__方法,子类必须显示调用基类的__init__。
没有返回值,否则会再引发TypeError错误。
http://www.cnblogs.com/itaceo-o/p/3300289.html
__init__:创建完对象后调用,对当前对象的一些实例初始化,无返回值
1、在类中,如果__new__和__init__同时存在,会优先调用__new__
>>> class Data(object): ... def __new__(self): ... print "new" ... def __init__(self): ... print "init" ... >>> data = Data() new
2、__new__方法会返回所构造的对象,__init__则不会。__init__无返回值。
>>> class Data(object): ... def __init__(cls): ... cls.x = 2 ... print "init" ... return cls ... >>> data = Data() init Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __init__() should return None, not 'Data'
>>> class Data(object): ... def __new__(cls): ... print "new" ... cls.x = 1 ... return cls ... def __init__(self): ... print "init" ... >>> data = Data() new >>> data.x =1 >>> data.x 1
If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().
如果__new__返回一个对象的实例,会隐式调用__init__
If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.
如果__new__不返回一个对象的实例,__init__不会被调用
<class '__main__.B'> >>> class A(object): ... def __new__(Class): ... object = super(A,Class).__new__(Class) ... print "in New" ... return object ... def __init__(self): ... print "in init" ... >>> A() in New in init <__main__.A object at 0x7fa8bc622d90> >>> class A(object): ... def __new__(cls): ... print "in New" ... return cls ... def __init__(self): ... print "in init" ... >>> a = A() in New
object.__init__(self[, ...])
Called when the instance is created. The arguments are those passed to the class constructor expression. If a base class has an __init__() method, the derived class’s __init__() method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: BaseClass.__init__(self, [args...]). As a special constraint on constructors, no value may be returned; doing so will cause a TypeError to be raised at runtime.
在对象的实例创建完成后调用。参数被传给类的构造函数。如果基类有__init__方法,子类必须显示调用基类的__init__。
没有返回值,否则会再引发TypeError错误。
http://www.cnblogs.com/itaceo-o/p/3300289.html
相关文章推荐
- Python __init__与__new__区别
- python __init__和__new__之间的区别
- python __init__和__new__之间的区别
- python中__new__和__init__的区别
- 【python】__new__和__init__区别
- 【python】__new__和__init__的区别
- 深入理解Python中的 __new__ 和 __init__及区别介绍
- Python中__init__和__new__的区别详解
- python __init__和__new__之间的区别
- Python中__new__与__init__方法的区别详解
- python实现单例模式__init__ __new__区别
- Python中__new__与__init__方法的区别详解
- 34 Python __new__与__init__区别
- Python中__init__和__new__的区别详解
- python中的__new__方法与__init__方法区别与调用;通过使用__new__实现单例模式
- Python中__new__与__init__方法的区别详解
- Python中__init__和__new__的区别详解
- python中__init__()方法和__new__()方法的区别
- Python的__new__与__init__区别
- Python中__init__和__new__的区别详解