unexpected behaviour in class variables

Gordon McMillan gmcm at hypernet.com
Mon Jun 26 19:32:50 EDT 2000


Andrew Dalke talks himself into a circle: 

[skipping much]

>import math
>def eggs(x):
> pass
>
>class Spam:
> builtin_func = math.cos
> def_func = eggs
> def p(self):
>  print self.builtin_func
>  print self.def_func
>  print math.cos

OK - builtin_func and def_func live in Spam.__dict__. Remember that <wink>.

>import math
>
>def eggs(x):
>    return math.sin(x)
>
>class Spam:
>    def __init__(self):
>        self.sin = eggs
>        self.cos = math.cos

Here, sin and cos (like wanton and lust) live in the instance dict.

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.

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

Make sense now?

- Gordon



More information about the Python-list mailing list