T.__new__(S, ...) -> a new object with type S, a subtype of T
这里的subtype指的是什么?
class A(int):
pass
class B(int):
def __new__(cls):
return A.__new__(int)
b = B()
isinstance(b, A)
#False
type(b)
#<class 'int'>
天蓬老师2017-04-17 17:52:46
Answer questions
subtype
literally means subclass
. Use issubclass(cls1, cls2)
to determine whether cls1
is < A subclass of code>cls2.subtype
如字面意思, 即子类
. 通过issubclass(cls1, cls2)
判断cls1
是不是cls2
的子类.
虽然不太确定你使用的python
版本, 以及你的T.__new__(S, ...)
是截取哪个文档, 但就官方说明, X.__init__(Y, ...)
就只是创建Y
的实例对象, 跟X
没有关系. 前提是X
有__new__
属性, 通过hasattr(X, '__new__')
判断.
深度探究
其实这里的重点并非subtype
, 而是__new__
到底是什么属性.
简单的说, __new__
控制对象的创建, 而__init__
控制对象的初始化(如添加额外的属性变量), __del__
控制对象的销毁. 正常情况下, 只需要在乎__init__
.
思考一个问题, 标准的实例化对象过程, 如n = N()
, 是否一定创建的是N
的对象?
答案当然是否定的, 代码如下, 以此来详细说明创建对象和初始化对象的区别.
class N:
def __init__(self, name):
self.name = name
class M:
def __new__(cls, name):
return N(name)
m = M("enalix")
isinstance(m, N) #=> true
isinstance(m, M) #=> false
m.name #=> "enalix"
当__new__
返回对象时, 会将对象及多余的参数, 传递给对象的__init__
, 以进行创建后的初始化.
关于函数
再进一步讲, __new__
的参数cls
是什么呢?
class P:
def __new__(cls):
return cls
P() #=> <class '__main__.P'>, 即类本身
P.__new__(P) #=> 同上
这涉及到python
的另一个本质特性, 即python
是没有方法的, 只有函数. 当执行P()
, 其实是先执行p1 = P.__new__(P)
, 再执行P.__init__(p1)
.
class X:
def hello(self):
return self.name
X.hello(m) #=> "enalix"
综上, T.__new__(S, ...)
, 跟T
没有关系, 就如hello
只是返回self
的name
属性, 跟X
没有关系类似.
额外说明
1, 关于python
只有函数的更多描述, 建议阅读本人的文章理解Ruby中的类.
2, 以上代码在python3
版本中通过验证, 2.X
python
you are using, and which document your T.__new__(S, ...)
is intercepting, but as far as the official explanation is concerned, X.__init__(Y, ...)
just creates an instance object of Y
and has nothing to do with X
. The premise is X
code> has the __new__
attribute, which can be determined by hasattr(X, '__new__')
.🎜
🎜🎜In-depth exploration🎜🎜
🎜In fact, the focus here is not subtype
, but what attribute __new__
is.🎜
🎜Simply put, __new__
controls the creation of objects, while __init__
controls the initialization of objects (such as adding additional attribute variables), and __del__
controls objects Destruction. Under normal circumstances, you only need to care about __init__
.🎜
🎜Think about a question, does the standard object instantiation process, such as n = N()
, necessarily create an object of N
?🎜
🎜The answer is of course no, the code is as follows to explain in detail the difference between creating an object and initializing an object.🎜
rrreee
🎜When __new__
returns an object, the object and extra parameters will be passed to the object's __init__
for initialization after creation.🎜
🎜🎜About functions🎜🎜
🎜Going a step further, what is the parameter cls
of __new__
?🎜
rrreee
🎜This involves another essential feature of python
, that is, python
has no methods, only functions. When executing P()
, it actually First execute p1 = P.__new__(P)
, then execute P.__init__(p1)
.🎜
rrreee
🎜To sum up, T.__new__(S, ...)
has nothing to do with T
, just like hello
just returns self
’s name
attribute has nothing to do with X
.🎜
🎜🎜Extra instructions🎜🎜
🎜1. For more description of python
only functions, it is recommended to read my article to understand classes in Ruby.🎜
🎜2, The above code has been verified in python3
version, there are slight differences in 2.X
🎜