Python真的为每个新实例都创建了所有绑定方法吗? [英] Does Python really create all bound method for every new instance?

查看:42
本文介绍了Python真的为每个新实例都创建了所有绑定方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关Python(3.4)中的类的信息,据我了解,似乎每个新对象都有其自己的绑定方法实例.

I am reading about classes in Python (3.4) and from what I understand it seems that every new object has its own bound methods instances.

class A:

    def __init__(self, name):
        self.name = name

    def foo(self):
        print(self.name)

a = A('One')
b = A('Two')

print(a.foo ==  b.foo)

它的输出是False.

在我看来,这似乎是在浪费记忆.我认为内部a.foob.foo会在内部以某种方式指向内存中的一个函数:A.foo其中将传递self作为类实例.

This seems to me as a waste of memory. I thought that internally a.foo and b.foo would point somehow internally to one function in memory: A.foo where self as the class instance will be passed.

我认为这可能无法用该语言轻松实现.

I assume this maybe cannot be implemented easily in the language.

每个新实例是否还包含其绑定方法的新实例?

Does each new instance contain also new instances of its bound methods?

如果是这样,这是否会比在其他语言(如Java)之间在对象之间共享"方法的其他语言更加谨慎地创建新对象,从而不会影响性能或使情况变得必要?

If so, does not this hurt the performance or make case for creating new objects more cautiously than in other languages where methods are "shared" among objects like in Java?

推荐答案

每次访问方法时,方法都是按需绑定的. .

Methods are bound on demand, each time you access one.

访问函数名称将调用描述符协议,该协议在函数对象上返回绑定的方法

Accessing the name of a function invokes the descriptor protocol, which on function objects returns a bound method.

绑定方法是功能对象周围的薄包装;它存储对原始函数和实例的引用.调用方法对象时,它依次将调用传递给函数,并插入实例作为第一个参数.

A bound method is a thin wrapper around a function object; it stores a reference to the original function and to the instance. When calling a method object, it in turn passes the call to the function, with instance inserted as a first argument.

创建实例时不会创建方法,因此不需要任何额外的内存.

Methods are not created when the instance is created, so there is no extra memory required a-priori.

您可以手动重新创建步骤:

You can re-create the steps manually:

>>> class A:
...     def __init__(self, name):
...         self.name = name
...     def foo(self):
...         print(self.name)
... 
>>> a = A('One')
>>> a.foo
<bound method A.foo of <__main__.A object at 0x100a27978>>
>>> a.foo.__self__
<__main__.A object at 0x100a27978>
>>> a.foo.__func__
<function A.foo at 0x100a22598>
>>> A.__dict__['foo']
<function A.foo at 0x100a22598>
>>> A.__dict__['foo'].__get__(a, A)
<bound method A.foo of <__main__.A object at 0x100a27978>>
>>> A.__dict__['foo'].__get__(a, A)()
One

每次都只重新创建方法对象;基本功能保持稳定:

It is only the method object that is recreated each time; the underlying function remains stable:

>>> a.foo is a.foo
False
>>> b = A('Two')
>>> b.foo is a.foo
False
>>> b.foo.__func__ is a.foo.__func__
True

此架构还使 classmethod property 对象起作用.您可以创建自己的描述符,创建大量有趣的绑定行为.

This architecture also makes classmethod, staticmethod, and property objects work. You can create your own descriptors, creating a whole host of interesting binding behaviours.

这篇关于Python真的为每个新实例都创建了所有绑定方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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