Dynamically created objects

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Dec 28 02:40:05 EST 2007


En Fri, 28 Dec 2007 04:14:43 -0300, Michael Bernhard Arp Sørensen  
<murderbystealth at gmail.com> escribió:

> I need to create objects on the fly in my program. The names of the
> objects must be unique, obviously, and I need to put them in a list for
> later use.
>
> How do i set the name of an object if the name is stored in another
> variable?
>
> I've looked in the O'Reiley Python Cookbook and on google, but no joy.

Use a dictionary as a container.

py> ns = {}
py> name = "Joe"
py> o = object()
py> ns[name] = o
py> another = set("another")
py> ns["another"] = another
py> ns
{'Joe': <object object at 0x009D0478>,
  'another': set(['a', 'e', 'h', 'o', 'n', 'r', 't'])}

-- 
Gabriel Genellina




More information about the Python-list mailing list