Re: naming objects from string



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/

.



Relevant Pages

  • Re: Bracketed types
    ... keywords of the intrinsic types of VB, the namespace containing the custom type is imported, and you want to ... Dim s As [String] ' Binds to the custom 'String' class. ... Using brackets in VB.NET helps with this and also to address conflicts with inherited keywords, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: XmlSerializer
    ... XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces; ... //Serialize the array, using the empty namespace ... > public class FormField { ... > public FormField(string Name, string Value) { ...
    (microsoft.public.dotnet.xml)
  • Re: XslTransform is not matching node when source XML contains an xmlns attribute
    ... >> in my XSLT. ... > element) are in the namespace with namespace URI ... > If you try to decode a byte stream like a memory stream to a string then ... > string yourself if all you want is a string, ...
    (microsoft.public.dotnet.xml)
  • Re: The Nature of God- String Theory
    ... Bosonic string theories are 26-dimensional, ... And so in HighSchoolMath (a namespace) we say "space is 3D because ... Fuller was present at the birth of "D talk" in the early 1900s. ... Ivory Towerites (i.e. we don't need their permission to roll out our ...
    (soc.religion.quaker)
  • Re: Bracketed types
    ... keywords of the intrinsic types of VB, the namespace containing the custom type is imported, and you want to use the type without qualifying it by its namespace: ... Dim s As [String] ' Binds to the custom 'String' class. ...
    (microsoft.public.dotnet.languages.vb)