方法对象与函数对象,Python 类实例与类 [英] method objects vs function objects , Python class instances vs class
问题描述
我正在尝试验证日期为 2012 年 11 月 1 日的 Python 教程 2.7.3 版第 9 章:类,第 66 页最后一行(source):
<块引用>实例对象的有效方法名称取决于它的类.经过定义,作为函数对象的类的所有属性定义其实例的相应方法.所以在我们的例子中,x.f 是一个有效的方法引用,因为 MyClass.f 是一个函数,但 x.i 不是,因为 MyClass.i 不是.但是 x.f 和 MyClass.f 不是一回事——它是一个方法对象,而不是一个函数对象.
我有这个:
class MyClass:"""一个简单的示例类"""我 = 12345定义 f():返回你好世界"
然后我这样做:
<预><代码>>>>x = MyClass()>>>xf<在0x02BB8968处<__main__.MyClass实例的绑定方法MyClass.f>>>>>我的类<未绑定方法 MyClass.f>>>>类型(MyClass.f)<输入'实例方法'>>>>类型(x.f)<输入'实例方法'>注意x.f
和MyClass.f
的类型都是instancemethod.类型没有区别,但教程另有说明.有人可以澄清一下吗?
Bound vs Unbound 方法 - 解释.
...或者为什么 Python 有你指出的行为.
首先,请注意这在 3.x 中有所不同.在 3.x 中,您将获得 MyClass.f
作为函数,而 x.f
作为方法 - 正如预期的那样.这种行为本质上是一个糟糕的设计决策,后来被改变了.
这样做的原因是 Python 有一个方法的概念,它不同于大多数语言,它本质上是一个函数,其中第一个参数被预填充为实例(self
).这种预填充形成了一个绑定方法.
在 Python 2.x 及更早版本中,不附加到实例的方法被认为是 未绑定方法,这是一个函数,其限制是第一个参数(self
),必须是对象的一个实例.然后准备好绑定到实例并成为绑定方法.
随着时间的推移,很明显未绑定的方法实际上只是一个具有这种奇怪限制的函数,这并不重要(self
必须属于 '正确' 类型),因此它们已从语言中删除(在 3.x 中).这本质上是鸭子类型self
,适合语言.
Python 3.3.0(默认,2012 年 12 月 4 日,00:30:24)>>>x.foo<在0x100858ed0处<__main__.MyClass对象的绑定方法MyClass.foo>>>>>MyClass.foo<函数 MyClass.foo 在 0x10084f9e0>
进一步阅读.
这是一个(从记忆中浓缩的)解释,可以从 Python 创建者 Guido van Rossum 自己的嘴里完整阅读 他的Python 历史"系列.
I am trying to verify the difference between instance attributes and class attributes as laid out by the Python tutorial release 2.7.3 dated Nov 01, 2012, chapter 9: Classes, Page 66 last line (source):
Valid method names of an instance object depend on its class. By definition, all attributes of a class that are function objects define corresponding methods of its instances. So in our example, x.f is a valid method reference, since MyClass.f is a function, but x.i is not, since MyClass.i is not. But x.f is not the same thing as MyClass.f — it is a method object, not a function object.
I have this:
class MyClass:
"""A simple example class"""
i = 12345
def f():
return 'hello world'
Then I do this:
>>> x = MyClass()
>>> x.f
<bound method MyClass.f of <__main__.MyClass instance at 0x02BB8968>>
>>> MyClass.f
<unbound method MyClass.f>
>>> type(MyClass.f)
<type 'instancemethod'>
>>> type(x.f)
<type 'instancemethod'>
Note that the type of both x.f
and MyClass.f
is instancemethod. There is no difference in types but the tutorial says otherwise. Can someone please clarify?
Bound vs Unbound Methods - an explanation.
... or why Python has the behaviour you point out.
So, first off, a note that this is different in 3.x. In 3.x, you will get MyClass.f
being a function, and x.f
as a method - as expected. This behaviour is essentially a poor design decision that has later been changed.
The reason for this is that Python has the concept of a method that is different to most languages, which is essentially a function with the first argument pre-filled as the instance (self
). This pre-filling makes a bound method.
>>> x.foo
<bound method MyClass.foo of <__main__.MyClass instance at 0x1004989e0>>
In Python 2.x and before, it was reasoned that a method not attached to an instance would be an unbound method, which was a function with the restriction that the first argument (self
), must be an instance of the object. This is then ready to be bound to an instance and become a bound method.
>>> MyClass.foo
<unbound method MyClass.foo>
With time, it became clear an unbound method is really just a function with this odd restriction that didn't really matter (that self
must be of the 'correct' type), so they were removed from the language (in 3.x). This is essentially duck-typing self
, which suits the language.
Python 3.3.0 (default, Dec 4 2012, 00:30:24)
>>> x.foo
<bound method MyClass.foo of <__main__.MyClass object at 0x100858ed0>>
>>> MyClass.foo
<function MyClass.foo at 0x10084f9e0>
Further reading.
This is a (condensed, from memory) explanation which can be read in full from Python creator Guido van Rossum's own mouth in his 'History of Python' series.
这篇关于方法对象与函数对象,Python 类实例与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!