unexpected behaviour in class variables
Gordon McMillan
gmcm at hypernet.com
Tue Jun 27 16:25:41 EDT 2000
Andrew Dalke wrote:
>Gordon McMillan wrote:
>>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?
Terminology sherminology!
>>> def eggs():
... pass
...
>>> class A:
... pass
...
>>> a = A()
>>> a.eggs = eggs
>>> type(a.eggs)
<type 'function'>
>>>
It ain't wrapped. Beat up Fred, he needs it <wink>.
>>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.)
I'm not really understanding.
>>> class A:
... pass
>>> A.sin = math.sin
>>> def eggs(x):
... print "eggs"
...
>>> A.eggs = eggs
>>> a = A()
>>> a.eggs()
eggs
>>> a.sin(0)
0.0
Functionally, the difference is the need to provide an arg to the user-
defined function that will take the value of "self". Or, perhaps, the
inability to use one in a C extension. Maybe your original post spelled out
some brilliantly deduced logical consequences of this apparently innocuous
difference. If so, they slid right past me <wink>.
- Gordon
More information about the Python-list
mailing list