从类中的一个方法继承的两个方法在实例中是不同的,不是吗? [英] Two methods inherited from one method in class are different in instances, aren't they?

查看:101
本文介绍了从类中的一个方法继承的两个方法在实例中是不同的,不是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么会这样吗?

  class Foo:
def bar(self):
传递

a = Foo()
b = Foo()

a.bar == b.bar #False
a.bar是b.bar #False

我认为它们都继承了类方法,而且它是一种方法。 / p>

解决方案

当您通过类中定义的实例访问函数时, bound-method 每次都创建对象。来自 docs


调用方法时到底发生了什么?您可能已经注意到
,即使 f的
函数定义(不包含上述参数),也调用 xf(), )
指定了一个参数。论证
怎么了?当
需要参数的函数没有任何参数时,Python肯定会引发异常 - 即使实际上没有使用参数
......



实际上,您可能已经猜到了答案:
方法的特殊之处在于实例对象作为函数的
的第一个参数传递。在我们的示例中,调用 x.f()完全等同于
MyClass.f(x)。通常,使用n个参数
的列表调用方法等效于使用参数
list调用相应的函数,该参数是通过在
第一个参数之前插入方法的实例对象而创建的。 / p>

如果您仍然不了解方法的工作原理,那么查看
实现可能会澄清问题。 当引用不是数据属性的实例属性
时,将搜索其类。如果
名称表示作为函数对象的有效类属性,则通过打包(指向)实例对象
和在抽象中一起找到的函数对象来创建
方法对象。 object:
这是方法对象。


注意,每次访问时都会发生方法

 >>> Foo类:
... def bar(self):传递
...
>>> f = Foo()
>>> f.bar是f.bar
False

这是如何工作的?实际上,函数是描述符对象 ,请注意是否存在 __ get __

  >>> def func():传递
...
>>>函数对象的func .__ get__
< method-wrapper'__get__'在0x101e38ae8>

换句话说,您可以将Python功能视为如此实施:

  class函数(对象):
。 。 。
def __get __(self,obj,objtype = None):
在Objects / funcobject.c中模拟func_descr_get()如果obj为None,则为

返回self
返回types.MethodType(self,obj)

当然,它们不是用Python实现的(至少在CPython!)。



另外请注意,直接在课堂上访问功能当然不会这样做:

 >>> Foo.bar是Foo.bar 
True


Can someone please explain why it is so?

class Foo:
    def bar(self):
        pass

a = Foo()
b = Foo()

a.bar == b.bar # False
a.bar is b.bar # False

I thought that they both inherit the class method and it's the one method.

解决方案

When you access a function through an instance that is defined on the class, a bound-method object is created each time. From the docs:

What exactly happens when a method is called? You may have noticed that x.f() was called without an argument above, even though the function definition for f() specified an argument. What happened to the argument? Surely Python raises an exception when a function that requires an argument is called without any — even if the argument isn’t actually used…

Actually, you may have guessed the answer: the special thing about methods is that the instance object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s instance object before the first argument.

If you still don’t understand how methods work, a look at the implementation can perhaps clarify matters. When an instance attribute is referenced that isn’t a data attribute, its class is searched. If the name denotes a valid class attribute that is a function object, a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object.

Note, this happens every time you access a method:

>>> class Foo:
...    def bar(self): pass
...
>>> f = Foo()
>>> f.bar is f.bar
False

How does this work? Well, actually, functions are descriptor objects, note the presence of a __get__:

>>> def func(): pass
...
>>> func.__get__
<method-wrapper '__get__' of function object at 0x101e38ae8>

In other words, you can think of Python funtions as being implemented thusly:

class Function(object):
    . . .
    def __get__(self, obj, objtype=None):
        "Simulate func_descr_get() in Objects/funcobject.c"
        if obj is None:
            return self
        return types.MethodType(self, obj)

Of course, they aren't implemented in Python (in CPython at least!).

Also note, accessing the funtion directly on the class, of course, doesn't do this:

>>> Foo.bar is Foo.bar
True

这篇关于从类中的一个方法继承的两个方法在实例中是不同的,不是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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