Create object for item in list

Terry Reedy tjreedy at udel.edu
Thu Apr 29 16:43:48 EDT 2004


"Helge" <helge at hefre.com> wrote in message
news:39885b3c.0404291110.7bd152ba at posting.google.com...
> I wonder how this can be accomplished:
>
> I've got a list, containing several strings. The strings look like
> this:
>
> [ "Name1/N1/N2/N3" , "Name2/N1/N2/N3" , "..." ]
>
> I would like to create one object for each item in the list, and the
> name of the object should be "str(item.split("/")[0])".

Binding and using dynamic names in the global (or function local) namespace
is generally a nuisance.  The standard idiom is to use a separate
dictionary as a namespace for just those items.  Something like

myobs = {}
for datastring in namedata:
    data = datastring.split('/')
    myobs[data[0]] = myclass(*data[1:])

Terry J. Reedy







More information about the Python-list mailing list