unexpected behaviour in class variables

Andrew Dalke dalke at acm.org
Mon Jun 26 21:25:40 EDT 2000


Gordon McMillan wrote in message
<8F5FCA994gmcmhypernetcom at 199.171.54.194>...
>Andrew Dalke talks himself into a circle:


Circle?  I was thinking more of a drunk^Wrandom walk :)

>An attribute found in the instance dict will never get wrapped. Only stuff
>found in the class __dict__ (or in a base class __dict__) qualifies for
>wrapping.


But the documentation at http://python.org/doc/current/ref/types.html says
(with some spacing changes to enforce better alignment)

: User-defined method objects are created in two ways: when getting
:  an attribute  of a class          that is a user-defined function object,
: or when getting
:  an attributes of a class instance that is a user-defined function object.

Isn't an "instance dict" in your terminology the same as "attributes of
a class instance" in the quoted documentation?  In which case, the docs
do not agree you or with the observed behaviour.  I have no problem with
that, in which case I'll ask the docs people to fix the docs.  My question
was to make sure that this really is a doc problem and not my
misunderstanding of terminology or behaviour.

>Why the wrapping? To hide away "self" - so it's implicit to the method
>object but explicit to the function object.
>
>Make sense now?


No.  Why does it only occur for user-defined function objects added to
the class namespace and not C-based functions or callable objects?  That
limits how I design my code if I want the ability to change between a
C-based implementation, a Python-based one or to a function object, which
is exactly the problem I have.  (I replaced a call to a C extension
library to a Python based one used for regression tests.)

It suggests that Python methods are defined as "user-defined functions
which are accessed in class namespace" rather than as I thought "functions
which were tagged by the compiler/importer to be methods" (ie, user-defined
functions which were created in a class scope or C extension methods which
told the run-time that they were methods.)

I did not expect the behaviour and now, even knowing that it occurs,
I cannot give a good explanation for the difference other than perhaps
it simplified implementation.

                    Andrew
                    dalke at acm.org







More information about the Python-list mailing list