detmining name during an assignment

Christian Heimes lists at cheimes.de
Fri Sep 18 19:38:08 CEST 2009


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.
> So, if a user says:
> 
> cube1 = Cube()
> 
>  A "cube1" object should apear in the scene list. I believe this means I need
> to determine that a namespace assignment is going on from inside the
> object's init code
> or by using properties.

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):
            value.name = name
            value.scene = self

class SceneObject(object):
    pass

class Cube(SceneObject):
    pass

scene = Scene()
scene.cube1 = Cube()

Have fun!

Christian




More information about the Python-list mailing list