class closure question

Steven W. Orr steveo at syslang.net
Thu Jan 17 10:22:14 EST 2008


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?

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



More information about the Python-list mailing list