class closure question
Peter Otten
__peter__ at web.de
Thu Jan 17 11:10:41 EST 2008
Steven W. Orr wrote:
> I want to indirectly change the value of a variable.
>
> #! /usr/bin/python
> foo = [44]
> bar = foo
> bar[0] = 55
> print 'bar = ', bar
> print 'foo = ', foo
>
> This works fine.
>
> bar = [55]
> foo = [55]
>
> But I want to do the same with a class value.
>
> #! /usr/bin/python
> S = None
> dd = { 'class': [S] }
> class C1(object):
> def __init__(self):
> print 'Hello from C1'
>
> def mkclass(base):
> class zzz(base):
> pass
> return zzz
>
> dd['class'][0] = mkclass( C1 )
> print "dd['class'][0].__bases__ = ", dd['class'][0].__bases__
> print 'S = ', S
>
> The answer is not what I want:
>
> dd['class'][0].__bases__ = (<class '__main__.C1'>,)
> S = None
>
> The goal is for S to be set to the returned class from mkclass.
>
> Can someone help?
What you want is not possible in Python. You can modify some objects
(called "mutable") but rebinding a name has to be explicit.
Peter
More information about the Python-list
mailing list