python __new__
Posted Lippman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python __new__相关的知识,希望对你有一定的参考价值。
new
This method is only used for new-style classes (classes inheriting from object).
Called to create a new instance of class cls. new is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of new should be the new object instance (usually an instance of cls).
Typical implementations create a new instance of the class by invoking the superclass’s new method using “super(currentclass, cls).new(cls[, …])” with appropriate arguments and then modifying the newly-created instance as necessary before returning it.
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.
If new does not return an instance of cls, then the new instance’s init method will not be invoked.
[new]] is intended mainly to allow subclasses of immutable types (like [int, str, or tuple) to customize instance creation.
- 静态方法
- 构建cls对象
- 如果__new__返回cls实例,__init__将被调用
- 如果__new__返回为空,__init__不会被调用
- init 实例方法,初始化实例
实验
实验1
- 重写new 但是不返回实例
- code
class A(object):
a = 1
def __new__(cls, *args, **kwargs):
print ‘+ A new‘
inst = super(A, cls).__new__(cls, *args, **kwargs)
print ‘- A new‘
#return inst
def __init__(self):
print ‘+ A init‘
print ‘- A init‘
a = A()
print type(a)
+ A new
- A new
<type ‘NoneType‘>
- 结论:
1 new无cls实例返回,初始化实例后将位None
2 init方法并没有被调用
实验2
- new 方法重写,调用超类的new并返回实例
class A(object):
a = 1
def __new__(cls, *args, **kwargs):
print ‘+ A new‘
inst = super(A, cls).__new__(cls, *args, **kwargs)
print ‘- A new‘
return inst
def __init__(self):
print ‘+ A init‘
print ‘- A init‘
a = A()
print type(a)
结果
+ A new
- A new
+ A init
- A init
<class ‘__main__.A‘>
应用:实现单例
class A(object):
_inst = None
def __new__(cls, *args, **kwargs):
print ‘+ A new‘
if not cls._inst:
print ‘do new.‘
cls._inst = super(A, cls).__new__(cls, *args, **kwargs)
cls._inst.inst_count = 0
print ‘- A new‘
return cls._inst
def __init__(self):
print ‘+ A init‘
self.inst_count += 1
print ‘- A init‘
a = A()
print type(a), a.inst_count
aa = A()
print type(aa), aa.inst_count
aaa = A()
print type(aaa), aaa.inst_count
- 结果
+ A new
do new.
- A new
+ A init
- A init
<class ‘__main__.A‘> 1
+ A new
- A new
+ A init
- A init
<class ‘__main__.A‘> 2
+ A new
- A new
+ A init
- A init
<class ‘__main__.A‘> 3
class A(object):
a = 1
def __new__(cls, *args, **kwargs):
print ‘+ A new‘
inst = super(A, cls).__new__(cls, *args, **kwargs)
print ‘- A new‘
#return inst
def __init__(self):
print ‘+ A init‘
print ‘- A init‘
a = A()
print type(a)
+ A new
- A new
<type ‘NoneType‘>
1 new无cls实例返回,初始化实例后将位None
2 init方法并没有被调用
- new 方法重写,调用超类的new并返回实例
class A(object):
a = 1
def __new__(cls, *args, **kwargs):
print ‘+ A new‘
inst = super(A, cls).__new__(cls, *args, **kwargs)
print ‘- A new‘
return inst
def __init__(self):
print ‘+ A init‘
print ‘- A init‘
a = A()
print type(a)
结果
+ A new
- A new
+ A init
- A init
<class ‘__main__.A‘>
应用:实现单例
class A(object):
_inst = None
def __new__(cls, *args, **kwargs):
print ‘+ A new‘
if not cls._inst:
print ‘do new.‘
cls._inst = super(A, cls).__new__(cls, *args, **kwargs)
cls._inst.inst_count = 0
print ‘- A new‘
return cls._inst
def __init__(self):
print ‘+ A init‘
self.inst_count += 1
print ‘- A init‘
a = A()
print type(a), a.inst_count
aa = A()
print type(aa), aa.inst_count
aaa = A()
print type(aaa), aaa.inst_count
- 结果
+ A new
do new.
- A new
+ A init
- A init
<class ‘__main__.A‘> 1
+ A new
- A new
+ A init
- A init
<class ‘__main__.A‘> 2
+ A new
- A new
+ A init
- A init
<class ‘__main__.A‘> 3
class A(object):
_inst = None
def __new__(cls, *args, **kwargs):
print ‘+ A new‘
if not cls._inst:
print ‘do new.‘
cls._inst = super(A, cls).__new__(cls, *args, **kwargs)
cls._inst.inst_count = 0
print ‘- A new‘
return cls._inst
def __init__(self):
print ‘+ A init‘
self.inst_count += 1
print ‘- A init‘
a = A()
print type(a), a.inst_count
aa = A()
print type(aa), aa.inst_count
aaa = A()
print type(aaa), aaa.inst_count
+ A new
do new.
- A new
+ A init
- A init
<class ‘__main__.A‘> 1
+ A new
- A new
+ A init
- A init
<class ‘__main__.A‘> 2
+ A new
- A new
+ A init
- A init
<class ‘__main__.A‘> 3
以上是关于python __new__的主要内容,如果未能解决你的问题,请参考以下文章