detmining name during an assignment

Jerry Hill malaclypse2 at gmail.com
Sun Sep 20 07:28:51 CEST 2009


On Fri, Sep 18, 2009 at 6:57 PM, Jamie Riotto <jamie.riotto at gmail.com> wrote:
> However, I'll have to keep looking for a more elegant solution.
> Telling a user that typing:
> cube1 = Cube(name = cube1) is a good thing because its pythonic is
> somehow unsatisfying.

That isn't pythonic.  The usual pythonic way to map names to objects
is to use one of python's most used datatypes: the dictionary.  So
they might do something like this:

scene["Cube1"] = Cube(xpos, ypos, zpos)

You could either have your Scene class inherit from dict, or write
custom __getattr__ and __setattr__ methods for it.  Alternatively, you
could add functions to your Scene class that manipulate a dictionary
that isn't directly exposed, and your users could do something like
this:

scene.add_item("Cube1", Cube(xpos, ypos, zpos))

Where the add_item method of the Scene would keep an internal
dictionary of all of the objects in the scene.

-- 
Jerry



More information about the Python-list mailing list