Re: Nested scopes and class variables

2005-02-03 Thread Steven Bethard
I wrote: Alex Martelli wrote: See the difference? In a function, the 'x = x' compiles into LOAD_FAST, STORE_FAST, which only looks at locals and nowhere else. In a classbody, it compiles to LOAD_NAME, STORE_NAME, which looks at locals AND globals -- but still not at closure cells... Is there a re

Re: Nested scopes and class variables

2005-02-03 Thread Dave Benjamin
Thanks, Nick and Alex, for the nice, detailed explanations. My understanding of Python bytecode is not deep enough to comment at this time. ;) -- .:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:. "talking about music is like dancing about architecture." -- http://m

Re: Nested scopes and class variables

2005-01-31 Thread Steven Bethard
Alex Martelli wrote: def f(x): ... class C: ... x = x ... return C ... [snip] def f(x): ... def g(): ... x = x ... return x ... return g ... [snip] See the difference? In a function, the 'x = x' compiles into LOAD_FAST, STORE_FAST, which only looks at locals and nowhere el

Re: Nested scopes and class variables

2005-01-31 Thread Alex Martelli
Dave Benjamin <[EMAIL PROTECTED]> wrote: > I ran into an odd little edge case while experimenting with functions that > create classes on the fly (don't ask me why): "Why not?". But classes have little to do with it, in my view. > >>> def f(x): > ... class C(object): > ... x = x Y

Re: Nested scopes and class variables

2005-01-31 Thread Nick Coghlan
[EMAIL PROTECTED] wrote: To me it seems you should do it something like this: -def f(x): -class C(object): -def __init__(self, x): -self.x = x # here you set the attribute for class C -c = C(x) # instantiate a C object -print c.x -f(5) That does something different -

Re: Nested scopes and class variables

2005-01-31 Thread Nick Coghlan
Dave Benjamin wrote: I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): It gets even kookier: Py> x = 5 Py> def f(y): ... class C(object): ... x = x ... print C.x ... Py> f(5) # OK with x bound at global 5 Py> def f(x): .

Re: Nested scopes and class variables

2005-01-31 Thread [EMAIL PROTECTED]
To me it seems you should do it something like this: -def f(x): -class C(object): -def __init__(self, x): -self.x = x # here you set the attribute for class C -c = C(x) # instantiate a C object -print c.x -f(5) -- http://mail.python.org/mailman/listinfo/python-lis

Nested scopes and class variables

2005-01-30 Thread Dave Benjamin
I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): >>> def f(x): ... class C(object): ... x = x ... print C.x ... >>> f(5) Traceback (most recent call last): File "", line 1, in ? File "", line 2, i