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 avoid
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)
is this possible?
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:
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 provide
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 here.
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
object object at 0x40077478
py locals()['bob'] = a
py bob
object object at 0x40077478
If you put this code within a
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), you
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
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,
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
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 (DO look it up!).
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
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 snake?)
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
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
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
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.
my program
17 matches
Mail list logo