[Python-Dev] On decorators implementation

"Martin v. Löwis" martin at v.loewis.de
Sun Aug 21 13:18:58 CEST 2005

Paolino wrote:
> I imagine that a method implementation of them inside the type metaclass
> could be better specified by people.

What you ask for is unimplementable. Method objects are created only
when the method is accessed, not (even) when the class is created.
Watch this:

>>> class X:
...   def foo(self):
...     pass
>>> x=X()
>>> type(x.foo)
<type 'instancemethod'>
>>> type(X.__dict__['foo'])
<type 'function'>

So even though the class has long been defined, inside X's dictionary,
foo is still a function. Only when you *access* x.foo, a method object
is created on the fly:

>>> x.foo is x.foo

Therefore, a decorator function cannot possibly get access to the method
object - it simply doesn't exist.


More information about the Python-Dev mailing list