本文介绍了Python 3中'函数','方法'和'绑定方法'有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 3中观察了至少3种与函数相关的类型:

 >>> class A():
... def f():pass
...
>>> A.f
>>> A().f
< bound method A.f of< __main __。0x7fcaef2fae80处的对象
>>> set.union
< method'union'of'set'objects>

我想知道'function','method'和'bound method'有什么区别?在Python 2中,'method'与'unbound method'相当吗?方法'类似于Python 2中的'unbound method'?

Kind-a-sort-a。但不是真的。它是用C代码定义的 method_descriptor 对象。这是一个未绑定的方法,但不是你在Python 2中找到的那种。



对于编写C的Python类型,所有'methods'都是C函数。你找到的'type'对象> 对象的< method'name'是一个特殊的对象,你可以用它来调用该函数给定一个实例和更多的参数,就像函数对象为自定义Python类提供。该对象在C中的。它像函数一样实现。



Python定义了其他几种这样的描述符类型;如果您使用,每个属性都是类型为 member_descriptor 的dsescriptor(参见) code> property staticmethod 可能是更好的已知描述符对象。

在Python 2中,绑定和非绑定方法实际上只是一种类型, instancemethod (由);如果设置了 __ self __ im_self )属性,它将报告为绑定。在使用函数作为描述符的Python 3中,如果没有 __ self __ set;而是调用函数.__获得__(),没有实例只是再次返回函数。



Python 2返回的唯一原因未绑定的方法是强制执行类型检查;第一个参数必须是该类(或其子类)的一个实例。对于支持鸭子输入的Python代码来说,这并没有什么意义,所以在Python 3中,限制被删除了。但是,对于C代码,您不能使用鸭子输入,您仍然必须限制类型,这就是为什么 C-type 仍返回 method_descriptor 执行此限制的对象。


I've observed at least 3 types related to functions in Python 3:

>>> class A():
...  def f(): pass
...
>>> A.f
<function A.f at 0x7fcaef304268>
>>> A().f
<bound method A.f of <__main__.A object at 0x7fcaef2fae80
>>> set.union
<method 'union' of 'set' objects>

I'm wondering what's the difference between 'function', 'method' and 'bound method'? Is 'method' a type equivalent to 'unbound method' in Python 2?

解决方案

Kind-a-sort-a. But not really. It is a method_descriptor object defined in C code. It is an unbound method, but not the kind you found in Python 2.

For Python types written C, all 'methods' are really C functions. The <method 'name' of 'type' objects> object you found is a special object you can use to call that function given an instance and further arguments, just like the function object does for custom Python classes. The object is defined in C in the PyMethodDescr_Type structure. It implements the descriptor protocol, just like functions do.

Python defines several other such descriptor types; if you use __slots__, each attribute is a dsescriptor of type member_descriptor (see the PyMemberDescr_Type structure), while classmethod, property and staticmethod are perhaps better known descriptor objects.

In Python 2, bound and unbound methods are really just one type, instancemethod (defined by the PyMethod_Type structure); it'll report as bound if the __self__ (im_self) attribute is set. In Python 3 using a function as a descriptor simply doesn't produce method objects without __self__ set; instead calling function.__get__() with no instance just returns the function again.

The only reason Python 2 returns unbound methods is to enforce a type check; the first argument must be an instance of the class (or a subclass thereof). This didn't make all that much sense for Python code that supports duck-typing, so in Python 3 the restriction was removed. However, with C code you can't use duck-typing, you still have to restrict the type, and that's why C-types still return a method_descriptor object that enforces this restriction.

这篇关于Python 3中'函数','方法'和'绑定方法'有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 00:11