术语:用户定义的函数对象属性?
Terminology: A user-defined function object attribute?
根据 Python 2.7.12 文档,User-defined methods:
User-defined method objects may be created when getting an attribute
of a class (perhaps via an instance of that class), if that attribute
is a user-defined function object, an unbound user-defined method
object, or a class method object. When the attribute is a
user-defined method object, a new method object is only created if the
class from which it is being retrieved is the same as, or a derived
class of, the class stored in the original method object; otherwise,
the original method object is used as it is.
我知道 Python 中的所有内容都是对象,因此 "user-defined method" 必须与 "user-defined method object" 相同。但是,我不明白为什么会有"user-defined function object attribute"。比如说,在下面的代码中:
class Foo(object):
def meth(self):
pass
meth
是在 class 主体内定义的函数,因此是 method。那为什么我们可以有一个"user-defined function object attribute"呢?不是所有属性都在 class 主体内定义吗?
悬赏问题:提供一些示例来说明如何通过getting[=]创建用户定义的方法对象31=] class 的属性。对象 没有在 class 定义中定义 吗? (我知道可以将方法分配给 class 实例,但那是猴子修补。)
我求助是因为这部分文档对我这个只会C的程序员来说真的很迷惑,因为Python是一门既支持函数式编程又支持面向对象的神奇语言程序员,我还没有掌握。我已经做了很多搜索,但仍然无法弄清楚。
当你这样做时
class Foo(object):
def meth(self):
pass
您正在使用方法 meth
定义 class Foo
。但是,当执行此 class 定义时,不会创建任何方法对象来表示该方法。 def
语句创建一个普通的函数对象。
如果你这样做
Foo.meth
或
Foo().meth
属性查找找到了函数对象,但是函数对象没有被用作属性的值。相反,使用 descriptor protocol、Python 调用函数对象的 __get__
方法来构造一个方法对象,该方法对象用作该查找的属性值。对于 Foo.meth
,方法对象是一个未绑定的方法对象,它的大部分行为类似于您定义的函数,但有一些额外的类型检查。对于Foo().meth
,方法对象是一个绑定的方法对象,它已经知道self
是什么
这就是为什么 Foo().meth()
不抱怨缺少 self
参数;您将 0 个参数传递给方法对象,然后将 self
添加到(空)参数列表并将参数传递给基础函数对象。如果 Foo().meth
直接计算到 meth
函数,则必须显式传递它 self
。
在Python3中,Foo.meth
不创建方法对象;函数的 __get__
仍然被调用,但它直接 returns 函数,因为他们认为未绑定的方法对象没有用。 Foo().meth
仍然会创建绑定方法对象。
根据 Python 2.7.12 文档,User-defined methods:
User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object, an unbound user-defined method object, or a class method object. When the attribute is a user-defined method object, a new method object is only created if the class from which it is being retrieved is the same as, or a derived class of, the class stored in the original method object; otherwise, the original method object is used as it is.
我知道 Python 中的所有内容都是对象,因此 "user-defined method" 必须与 "user-defined method object" 相同。但是,我不明白为什么会有"user-defined function object attribute"。比如说,在下面的代码中:
class Foo(object):
def meth(self):
pass
meth
是在 class 主体内定义的函数,因此是 method。那为什么我们可以有一个"user-defined function object attribute"呢?不是所有属性都在 class 主体内定义吗?
悬赏问题:提供一些示例来说明如何通过getting[=]创建用户定义的方法对象31=] class 的属性。对象 没有在 class 定义中定义 吗? (我知道可以将方法分配给 class 实例,但那是猴子修补。)
我求助是因为这部分文档对我这个只会C的程序员来说真的很迷惑,因为Python是一门既支持函数式编程又支持面向对象的神奇语言程序员,我还没有掌握。我已经做了很多搜索,但仍然无法弄清楚。
当你这样做时
class Foo(object):
def meth(self):
pass
您正在使用方法 meth
定义 class Foo
。但是,当执行此 class 定义时,不会创建任何方法对象来表示该方法。 def
语句创建一个普通的函数对象。
如果你这样做
Foo.meth
或
Foo().meth
属性查找找到了函数对象,但是函数对象没有被用作属性的值。相反,使用 descriptor protocol、Python 调用函数对象的 __get__
方法来构造一个方法对象,该方法对象用作该查找的属性值。对于 Foo.meth
,方法对象是一个未绑定的方法对象,它的大部分行为类似于您定义的函数,但有一些额外的类型检查。对于Foo().meth
,方法对象是一个绑定的方法对象,它已经知道self
是什么
这就是为什么 Foo().meth()
不抱怨缺少 self
参数;您将 0 个参数传递给方法对象,然后将 self
添加到(空)参数列表并将参数传递给基础函数对象。如果 Foo().meth
直接计算到 meth
函数,则必须显式传递它 self
。
在Python3中,Foo.meth
不创建方法对象;函数的 __get__
仍然被调用,但它直接 returns 函数,因为他们认为未绑定的方法对象没有用。 Foo().meth
仍然会创建绑定方法对象。