为什么一个方法与自身不一样? [英] Why is a method not identical to itself?

查看:169
本文介绍了为什么一个方法与自身不一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的Python文档是运算符说:


运算符不是测试
对象标识: x是y 是真的if和
只有 x y 是同一个对象。 x
不是y
产生相反的真值
值。

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.


$ b b

让我们试试:

Let's try that:

>>> def m():
...   pass
... 
>>> m is m
True

Python文档还说


自动垃圾收集,
自由列表和
描述符的动态性质,你可能会注意到
在某些用途中的异常行为
运算符,例如涉及
实例方法,
或常量之间的比较。请检查其
文档以获取更多信息。

Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info.



>>> class C:
...   def m():
...     pass
... 
>>> C.m is C.m
False

我搜索了更多解释,但我无法找到任何。

I searched for more explanations, but I was not able to find any.

为什么 Cm是Cm false?

我使用Python 2.x.如下面的答案所示,在Python 3.x Cm是Cm 是真的。

I am using Python 2.x. As noted in the answers below, in Python 3.x C.m is C.m is true.

推荐答案

当你要求一个实例的属性是一个函数,你会得到一个 bound方法:一个可调用的对象,它包装了类中定义的函数,并传递实例作为第一个参数。在Python 2.x中,当你要求一个类的属性,这是一个函数,你得到一个类似的代理对象调用一个未绑定的方法

When you ask for an attribute of an instance which is a function, you get a bound method: a callable object which wraps the function defined in the class and passes the instance as the first argument. In Python 2.x, when you ask for an attribute of a class which is a function you get a similar proxy object called an unbound method:

>>> class A: m = lambda: None
...
>>> A.m
<unbound method A.<lambda>>

这个特殊对象在你请求时创建,并且不会显式缓存到任何地方。这意味着当你做

This special object is created when you ask for it, and not apparently cached anywhere. That means that when you do

>>> A.m is A.m
False

您正在创建两个不同的方法对象并测试它们的身份。

you are creating two distinct unbound method objects and testing them for identity.

注意

>>> x = A.m
>>> x is x
True

>>> A.m.im_func is A.m.im_func
True

工作正常。 ( im_func 是未绑定方法对象包装的原始函数。)

work fine. (im_func is the original function which the unbound method object is wrapping.)

在Python 3.x中, , Cm is Cm 是True,因为(有点毫无意义)未绑定的方法代理对象被完全删除,你只是得到你定义的原始函数。

In Python 3.x, incidentally, C.m is C.m is True, because the (somewhat pointless) unbound method proxy objects were removed entirely and you just get the original function which you defined.

这只是Python中属性查找的动态性质的一个例子:当你要求一个对象的属性时,可以运行任意Python来计算该属性的值。这里是另一个例子,其中你的测试失败,其中更清楚为什么:

This is just one example of the very dynamic nature of attribute lookup in Python: when you ask for an attribute of an object, it is possible to run arbitrary Python to calculate the value of that attribute. Here's another example where your test fails in which it is much clearer why:

>>> class ChangingAttribute(object):
...     @property
...     def n(self):
...             self._n += 1
...             return self._n
...
...     def __init__(self):
...             self._n = 0
...
>>> foo = ChangingAttribute()
>>> foo.n
1
>>> foo.n
2
>>> foo.n
3
>>> foo.n is foo.n
False
>>> foo.n
6

这篇关于为什么一个方法与自身不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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