naming objects from string

manstey manstey at csu.edu.au
Thu Sep 21 00:33:56 EDT 2006


Hi,

But this doesn't work if I do:

a=object()
x='bob'
 locals()[x] = a

How can I do this?

James Stroud wrote:
> manstey wrote:
> > Hi,
> >
> > If I have a string, how can I give that string name to a python object,
> > such as a tuple.
> >
> > e.g.
> >
> > a = 'hello'
> > b=(1234)
> >
> > and then a function
> > name(b) = a
> >
> > which would mean:
> > hello=(1234)
> >
> > is this possible?
> >
>
> Depends on your namespace, but for the local namespace, you can use this:
>
> py> a = object()
> py> a
> <object object at 0x40077478>
> py> locals()['bob'] = a
> py> bob
> <object object at 0x40077478>
>
> A similar approach can be used for the global namespace.
>
> James
>
> --
> James Stroud
> UCLA-DOE Institute for Genomics and Proteomics
> Box 951570
> Los Angeles, CA 90095
> 
> http://www.jamesstroud.com/




More information about the Python-list mailing list