基于原型委托的模型如何在 Python 中工作?
How does Prototype Delegation Based model work in Python?
我曾经认为 python 跟在 ''Class Based Inheritance only (maybe my limited knowledge). But after reading JavaScript which shows the '' Prototype Delegation Based Model 之后,我意识到甚至 python 也显示了基于原型实现的一些特征
class A(object):
def __init__(self, a):
self.a = a
def square(self):
return self.a * self.a
a = A(10)
print(a.a) #10
A.b = 20 # a new Property of the class
print(a.b) # 20 - available via "delegation" for the "a" instance
同样,就像在基于原型的模型中一样,可以在运行时更改对象的原型。
class B(object):
""" Empty class B"""
pass
b = B() # an instance of the class B
b.__class__ = A # changing class (prototype) dynamically
print (b.__dict__) # {}
b.a = 20 # create a new property
print(b.square()) # 400 - method of the class A is available
即使我删除了对 class A
的显式引用
del A # deleting the explicit reference on classes
# print(A) --> error
Class A 的方法仍然可用于对象 b
print(b.square()) # 400
还有当我写b.__class__ = A
print(b.__class__) #<class '__main__.A'>
我的意思是当前实例是对 type A
的引用,对吗?
因为当我打印
print(A.__class__) # <type 'type'>
意思是A是type类型的引用。
由于 python 中的所有内容都是对象,我从未明确创建 A
的任何对象。此委托模型是否隐式创建 A
的对象?如果不是,那么这个模型是如何工作的?对象 b
是否保持对 A 或 A 的对象的隐式引用,以防它创建一个对象,如上所述?
Since everything in python is an object and i've never created any Object of A explicitly. Does This delegation model creates the Object Of A Implicitly ?
没有
If not then how does this model work ?
每个对象都知道它的 class(您可以更改)。属性查找是动态发生的:每次你尝试访问一个属性(包括像 .square()
这样的方法)时,Python 首先查看实例是否具有属性本身,然后查看任何 __class__
实例在那一刻,看着那里。
Does the object b maintain the implicit reference to A or object of A in case it creates an Object as asked above ?
当您执行 b.__class__ = A
时,您明确地向 b
提供了对 A
的引用。请注意 del A
而不是 删除名为 A 的 class;它会删除 name A
。由于有另一个对这个 class 的引用(即 b.__class__
),class 仍然存在并且所有查找将照常进行。
它在 Python 中的基本工作方式是每个对象都有一个其超class 列表,并且每次您尝试访问对象的属性时都会按顺序检查这些列表。您可以更改该列表(通过分配给 __class__
,甚至通过更改其 class 或 superclasses 的 __bases__
属性)。
我不太确定您认为这与原型继承有什么关系。关于原型继承的主要事情是 classes 和实例之间没有区别;您可以直接从任何其他实例创建 "instance"。这实际上不是 Python 的工作方式,因为 classes 和它们的实例具有不同的行为,你不能直接 "clone" 一个实例,就像你从 class.
class 共享它的底层机制
属性如下。
当您访问一个属性时,该值可能来自几个不同的地方。
首先检查实例,如果什么都不知道,则搜索移动到
instance 的 class 代替然后向上继承
链。例如,obj.attr
可能是实例 obj
的 attr
属性,如果它是在 __init__
中设置的,或者稍后在 [=26] 中动态设置的 attr
=] 定义(典型的方法)。
因此,如果您执行 obj.__class__ = A
,则可以使它引用 A 的属性,这些属性是内存中的对象。 del A
使名称 A
不可用,但是 对象只有在变得不可访问时才会被垃圾回收。 A
现在无法访问,但不是它的属性,因为它们可以通过 obj
访问。
我曾经认为 python 跟在 ''Class Based Inheritance only (maybe my limited knowledge). But after reading JavaScript which shows the '' Prototype Delegation Based Model 之后,我意识到甚至 python 也显示了基于原型实现的一些特征
class A(object):
def __init__(self, a):
self.a = a
def square(self):
return self.a * self.a
a = A(10)
print(a.a) #10
A.b = 20 # a new Property of the class
print(a.b) # 20 - available via "delegation" for the "a" instance
同样,就像在基于原型的模型中一样,可以在运行时更改对象的原型。
class B(object):
""" Empty class B"""
pass
b = B() # an instance of the class B
b.__class__ = A # changing class (prototype) dynamically
print (b.__dict__) # {}
b.a = 20 # create a new property
print(b.square()) # 400 - method of the class A is available
即使我删除了对 class A
的显式引用del A # deleting the explicit reference on classes
# print(A) --> error
Class A 的方法仍然可用于对象 b
print(b.square()) # 400
还有当我写b.__class__ = A
print(b.__class__) #<class '__main__.A'>
我的意思是当前实例是对 type A
的引用,对吗?
因为当我打印
print(A.__class__) # <type 'type'>
意思是A是type类型的引用。
由于 python 中的所有内容都是对象,我从未明确创建 A
的任何对象。此委托模型是否隐式创建 A
的对象?如果不是,那么这个模型是如何工作的?对象 b
是否保持对 A 或 A 的对象的隐式引用,以防它创建一个对象,如上所述?
Since everything in python is an object and i've never created any Object of A explicitly. Does This delegation model creates the Object Of A Implicitly ?
没有
If not then how does this model work ?
每个对象都知道它的 class(您可以更改)。属性查找是动态发生的:每次你尝试访问一个属性(包括像 .square()
这样的方法)时,Python 首先查看实例是否具有属性本身,然后查看任何 __class__
实例在那一刻,看着那里。
Does the object b maintain the implicit reference to A or object of A in case it creates an Object as asked above ?
当您执行 b.__class__ = A
时,您明确地向 b
提供了对 A
的引用。请注意 del A
而不是 删除名为 A 的 class;它会删除 name A
。由于有另一个对这个 class 的引用(即 b.__class__
),class 仍然存在并且所有查找将照常进行。
它在 Python 中的基本工作方式是每个对象都有一个其超class 列表,并且每次您尝试访问对象的属性时都会按顺序检查这些列表。您可以更改该列表(通过分配给 __class__
,甚至通过更改其 class 或 superclasses 的 __bases__
属性)。
我不太确定您认为这与原型继承有什么关系。关于原型继承的主要事情是 classes 和实例之间没有区别;您可以直接从任何其他实例创建 "instance"。这实际上不是 Python 的工作方式,因为 classes 和它们的实例具有不同的行为,你不能直接 "clone" 一个实例,就像你从 class.
class 共享它的底层机制
属性如下。
当您访问一个属性时,该值可能来自几个不同的地方。
首先检查实例,如果什么都不知道,则搜索移动到
instance 的 class 代替然后向上继承
链。例如,obj.attr
可能是实例 obj
的 attr
属性,如果它是在 __init__
中设置的,或者稍后在 [=26] 中动态设置的 attr
=] 定义(典型的方法)。
因此,如果您执行 obj.__class__ = A
,则可以使它引用 A 的属性,这些属性是内存中的对象。 del A
使名称 A
不可用,但是 对象只有在变得不可访问时才会被垃圾回收。 A
现在无法访问,但不是它的属性,因为它们可以通过 obj
访问。