detmining name during an assignment

Gabriel Genellina gagsl-py2 at
Fri Sep 18 22:10:24 CEST 2009

En Fri, 18 Sep 2009 14:38:08 -0300, Christian Heimes <lists at>  
> Jamie Riotto schrieb:

>> I have an app that uses Python scripting. When a user creates a new  
>> object:
>> objName = newObject()
>> I'd like the newObject be able to use objName as its internal name.
> As the others already explained to you there is no way to archive your
> goal with an assignment to a local or global variable. But you can
> follow a different approach:
> class Scene(object):
>     def __setattr__(self, name, value):
>         super(Scene, self).__setattr__(name value)
>         if isinstance(value, SceneObject):
>    = name
>             value.scene = self
> class SceneObject(object):
>     pass
> class Cube(SceneObject):
>     pass
> scene = Scene()
> scene.cube1 = Cube()

As the OP said it's being used for scripting some application, presumably  
the application can control the environment on which the script is run.  
One may use the Scene class above as the globlal scope when executing the  

scene = Scene()
code = "cube1 = Cube(); print"
exec code in Scene

(well, not exactly, Scene should inherit from dict and override  
__setitem__ instead, but you get the idea)

Gabriel Genellina

More information about the Python-list mailing list