Re: naming objects from string

2006-09-21 Thread Roberto Bonvallet
manstey wrote: [...] > bob_apple=() > bob_orange=() > .. > pete_red=() > > I then populate the 9 tuples with data [...] You cannot "populate" a tuple. If you want to insert the values individually, you have to use a list. If you insert them all together, like this: bob_apple = (1, 2, ..., 9),

Re: naming objects from string

2006-09-21 Thread Terry Reedy
"James Stroud" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Depends on your namespace, but for the local namespace, you can use this: > > py> a = object() > py> a > > py> locals()['bob'] = a > py> bob > If you put this code within a function, it probably will not work. locals

Re: naming objects from string

2006-09-21 Thread Jeremy Sanders
manstey wrote: > so they might provide a list of names, like 'bob','john','pete', with 3 > structures per name, such as 'apple','orange','red' and I need 9 tuples > in my code to store their data: > > bob_apple=() > bob_orange=() > .. > pete_red=() I really think you should be using dictionaries

Re: naming objects from string

2006-09-21 Thread Fredrik Lundh
manstey wrote: > so they might provide a list of names, like 'bob','john','pete', with 3 > structures per name, such as 'apple','orange','red' and I need 9 tuples > in my code to store their data: > > bob_apple=() > bob_orange=() > .. > pete_red=() > > I then populate the 9 tuples with data they

Re: naming objects from string

2006-09-21 Thread Gabriel Genellina
At Thursday 21/9/2006 00:59, manstey wrote: 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? You may use another object as a namespace: class

Re: naming objects from string

2006-09-20 Thread Tim Roberts
"manstey" <[EMAIL PROTECTED]> wrote: > >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) That's not a tuple. That's an integer. (1234,) is a tuple. >and then a function >name(b) = a > >which would mean: >hello=(1234) > >i

Re: naming objects from string

2006-09-20 Thread Ben Finney
"manstey" <[EMAIL PROTECTED]> writes: > If I have a string, how can I give that string name to a python > object, such as a tuple. The thing I'd like to know before answering this is: how will you be using that name to refer to the object later? -- \ "If you ever catch on fire, try to avoi

Re: naming objects from string

2006-09-20 Thread Wildemar Wildenburger
manstey wrote: > Hi, > > thanks for the suggestions. this is my problem: > > I have a metadata file that another user defines, and I don't know > their structure in advance. They might have 300+ structures. the > metadata defines the name and the tuple-like structure when read by > python. > > m

Re: naming objects from string

2006-09-20 Thread manstey
Hi, thanks for the suggestions. this is my problem: I have a metadata file that another user defines, and I don't know their structure in advance. They might have 300+ structures. the metadata defines the name and the tuple-like structure when read by python. my program reads in the metadata fil

Re: naming objects from string

2006-09-20 Thread manstey
Hi, thanks for the suggestions. this is my problem: I have a metadata file that another user defines, and I don't know their structure in advance. They might have 300+ structures. the metadata defines the name and the tuple-like structure when read by python. my program reads in the metadata fil

Re: naming objects from string

2006-09-20 Thread Wildemar Wildenburger
Damjan wrote: > try > sys.modules[__name__].__dict__[x] = a @manstay: You see! Ugly, unreadable trickery! Hands off this stuff, bad mojo! You've been told three very different approaches now, which is a pretty good indicator that there is no obvious way to do it. Which means another angle to yo

Re: naming objects from string

2006-09-20 Thread Wildemar Wildenburger
manstey wrote: > Hi, > > But this doesn't work if I do: > > a=object() > x='bob' > locals()[x] = a > > How can I do this? You can. I just copy/pasted your code and it works fine here. (You are aware that there is whitespace before locals() that you have to remove before you feed it to the sn

Re: naming objects from string

2006-09-20 Thread Damjan
manstey wrote: > Hi, > > But this doesn't work if I do: > > a=object() > x='bob' > locals()[x] = a > > How can I do this? try sys.modules[__name__].__dict__[x] = a But what's the point? -- damjan -- http://mail.python.org/mailman/listinfo/python-list

Re: naming objects from string

2006-09-20 Thread Wildemar Wildenburger
manstey wrote: > 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? > Direct answer: Look up the setattr() functions (D

Re: naming objects from string

2006-09-20 Thread manstey
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 t

Re: naming objects from string

2006-09-20 Thread James Stroud
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 l

naming objects from string

2006-09-20 Thread manstey
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? -- http://mail.python.org/mailman/listinfo/python-list