Re: class closure question



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
.



Relevant Pages

  • Re: explicit interface implementation does not work with events
    ... For some reason you need to perform explicit registration in this case ... Bar b = new Bar; ... private static void f_Foo ... EventHandler foo; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Insert with response
    ... FooBar, there's no way and no need to put them in synch. ... column in the foo table to 250 calumns in the bar table. ... set statistics time off ...
    (microsoft.public.sqlserver.programming)
  • Re: Magic function
    ... processing objects created in root at depth 3 ... root obj2 at depth 3 ... processing objects created in foo at depth 2 ... processing objects created in bar at depth 0 ...
    (comp.lang.python)
  • Re: from __future__ import absolute_import ?
    ... foo not in bar ... Unfortunately this is a side effect of using the os's directory structure to represent a python "package" structure. ...
    (comp.lang.python)
  • Re: from __future__ import absolute_import ?
    ... foo not in bar ... A path below the package level is generally a good means to shoot ... to represent a python "package" structure. ...
    (comp.lang.python)