Re: class closure question
- From: Peter Otten <__peter__@xxxxxx>
- Date: Thu, 17 Jan 2008 17:10:41 +0100
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
.
- References:
- class closure question
- From: Steven W. Orr
- class closure question
- Prev by Date: Re: Is this a bug, or is it me?
- Next by Date: Re: class closure question
- Previous by thread: class closure question
- Next by thread: Re: class closure question
- Index(es):
Relevant Pages
|