Building CPython
Marko Rauhamaa
marko at pacujo.net
Fri May 15 06:50:29 EDT 2015
Chris Angelico <rosuav at gmail.com>:
> On Fri, May 15, 2015 at 6:59 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> Must a method lookup necessarily involve object creation?
>
> Actually, no.
> [...]
> a particular Python implementation is most welcome to notice the
> extremely common situation of method calls and optimize it.
I'm not sure that is feasible given the way it has been specified. You'd
have to prove the class attribute lookup produces the same outcome in
consecutive method references.
Also:
>>> class X:
... def f(self): pass
...
>>> x = X()
>>> f = x.f
>>> ff = x.f
>>> f is ff
False
Would a compliant Python implementation be allowed to respond "True"?
Maybe. At least method objects seem immutable:
>>> f.__name__
'f'
>>> f.__name__ = 'g'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'method' object has no attribute '__name__'
>>> f.__eq__ = None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'method' object attribute '__eq__' is read-only
>>> f.xyz = 'xyz'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'method' object has no attribute 'xyz'
Marko
More information about the Python-list
mailing list