Confused with classmethods
Ruud de Jong
ruud<dot>de<dot>jong<at>consunet <dot>
Sat Mar 12 09:51:01 EST 2005
jfj schreef:
> Ruud wrote:
>
>>
>> So far for *how* it works. As to *why* it works like this, I don't know
>> for sure. But my guess is that the reasoning was something as follows:
>> if you define a function (regular or something special like a
>> classmethod) only for an instance of a class, you obviously don't
>> want to use it in a class context: it is -by definition- invisible to
>> the class, or to other instances of the same class.
>> One possible use case would be to store a callback function.
>> And in that case you definitely don't want the class magic to happen
>> when you reference the function.
>
>
>
> Yep. Got it. Indeed the reason seems to be a valid optimization:
> -in 99% of the cases you request something from an instance it is a
> plain old variable
> -in 99% of the cases you request something from a class it's a
> function.
>
> So it would be a waste of time to check for the conversion when
> something exists in the __dict__ of the instance, indeed.
>
>
> OTOH, I'm talking about the "concept of python" and not CPython
> implementation, and that's why I have these questions:)
>
>
> Thanks,
>
> jfj
>
I love this type of discussion -- it really forces me to do research
on my assumptions. But in this case I don't believe that this is
specific to CPython. From the Python Reference Manual, Section 3.2,
"The Standard Type Hierarchy", in the part about callable types,
user-defined methods:
<quote>
Note that the transformation from function object to (unbound or bound)
method object happens each time the attribute is retrieved from the
class or instance. In some cases, a fruitful optimization is to assign
the attribute to a local variable and call that local variable. Also
notice that this transformation only happens for user-defined functions;
other callable objects (and all non-callable objects) are retrieved
without transformation. It is also important to note that user-defined
functions which are attributes of a class instance are not converted to
bound methods; this only happens when the function is an attribute of
the class.
</quote>
So it seems that this mechanism is indeed fully part of the python
concept, not an optimization specific for CPython implementation.
When you define a function as an instance attribute (instead of
a class attribute), then none of the class magic happens.
When you access that attribute, you get the bare function, not a method.
--
'@'.join('.'.join(s) for s in (['ruud','de','jong'],['tiscali','nl']))
More information about the Python-list
mailing list