方法对象vs函数对象,Python类实例vs类 [英] method objects vs function objects , Python class instances vs class

查看:199
本文介绍了方法对象vs函数对象,Python类实例vs类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图验证实例属性和类属性之间的差异,如Python教程版本2.7.3(日期为2012年11月1日,第9章:类,最后一行( source ):

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):


实例对象的有效方法名称取决于它的类。通过
定义,作为函数对象的类的所有属性定义
其实例的对应方法。所以在我们的例子中,x.f是一个
有效的方法引用,因为MyClass.f是一个函数,但是x.i不是
,因为MyClass.i不是。 但是xf与MyClass.f -
不同,它是一个方法对象,而不是一个函数对象。

我有这个:

class MyClass:    
   """A simple example class"""    
   i = 12345   
   def f():    
      return 'hello world'

然后我这样做:

>>> 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'>

请注意, xf MyClass.f 是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?

推荐答案

绑定 vs 解释。



...或为什么Python有你指出的行为。



注意这是不同的3.x.在3.x中,你会得到 MyClass.f 是一个函数,而 x.f 作为一个方法 -

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.

这样做的原因是Python的方法概念与大多数语言不同,基本上是一个函数,第一个参数预先填充为实例( self )。

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>>

在Python 2.x和之前的版本中,我们推测没有附加到实例的方法一个非绑定方法,这是一个限制第一个参数( self )的函数必须是对象的一个​​实例。这样就可以绑定到一个实例,并成为一个绑定方法

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>

随着时间的推移,一个未绑定的方法真的只是一个具有这种奇怪限制的函数, ( self 必须是'正确'类型),所以它们从语言中删除(在3.x中)。这实际上是鸭式输入 self ,适合该语言。

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>



进一步阅读。



(缩写,从记忆)解释,可以从Python创建者Guido van Rossum自己的口中完全阅读他的Python历史系列

这篇关于方法对象vs函数对象,Python类实例vs类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆