472,119 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

programmatically define a new variable on the fly

Hi,

I would like to define a new variable which is not predefined by me.
For example,
I want to create an array called "X%s" where "%s" is to be determined
based on the data I am processing. So, for example, if I the file
I'm reading has
g 99
on the first line, I want to create a new variable called "Xg" whose
length
is 99.
I tried eval("Xg=[0]*99") but that did not work.

any help would be greatly appreciated
Lee

Aug 9 '07 #1
7 5650
On Aug 9, 5:11 pm, Lee Sander <lesa...@gmail.comwrote:
Hi,

I would like to define a new variable which is not predefined by me.
For example,
I want to create an array called "X%s" where "%s" is to be determined
based on the data I am processing. So, for example, if I the file
I'm reading has
g 99
on the first line, I want to create a new variable called "Xg" whose
length
is 99.
I tried eval("Xg=[0]*99") but that did not work.
eval only evaluates expressions. To go about the problem that way, you
would use exec, not eval.

Of course, use of exec generally means you're going about the problem
the wrong way; it looks to me like a better way to do what you're
doing is to create a dictionary to hold your values. So, given the
variables data (the dictionary), name (in your example, 'g') and
*size* (in your example, 99), you can add it data as shown:

data[name] = [0]
data[name] *= size

Note that by splitting the '[0]*size' code into two lines as shown
above, you don't create an intermediate list object that gets thrown
away right after creation.

Aug 9 '07 #2
On Aug 9, 5:30 pm, Dustan <DustanGro...@gmail.comwrote:
given the
variables data (the dictionary), name (in your example, 'g') and
*size* (in your example, 99), you can add it data as shown:
erm... make that:

given the variables data (the dictionary), name (in your example, 'g')
and size (in your example, 99), you can add it to data as shown:

Aug 9 '07 #3
Lee Sander wrote:
Hi,

I would like to define a new variable which is not predefined by me.
For example,
I want to create an array called "X%s" where "%s" is to be determined
based on the data I am processing. So, for example, if I the file
I'm reading has
g 99
on the first line, I want to create a new variable called "Xg" whose
length
is 99.
I tried eval("Xg=[0]*99") but that did not work.

any help would be greatly appreciated
Lee
>>Xg
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Xg' is not defined
>>namespace = globals()
var_name = 'Xg'
var_value = [0] * 9
namespace[var_name] = var_value
Xg
[0, 0, 0, 0, 0, 0, 0, 0, 0]

Ian

Aug 9 '07 #4
On Aug 9, 6:11 pm, Lee Sander <lesa...@gmail.comwrote:
I would like to define a new variable which is not predefined by me.
For example,
I want to create an array called "X%s" where "%s" is to be determined
based on the data I am processing.
Use a dictionary.

Aug 9 '07 #5
Lee Sander a écrit :
Hi,

I would like to define a new variable which is not predefined by me.
For example,
I want to create an array called "X%s" where "%s" is to be determined
based on the data I am processing. So, for example, if I the file
I'm reading has
g 99
on the first line, I want to create a new variable called "Xg" whose
length
is 99.
I tried eval("Xg=[0]*99") but that did not work.

any help would be greatly appreciated
As others already pointed out, the canonical solution is to use a dict,
and it's almost always the best solution - a variant being to use an
object (eventually a module object) and dynamically add attributes to it
using setattr()[1].

Now, for the corner case where you *really* want to dynamically create
new names in the module itself, the solution is to use exec. But I would
definitively *not* recommand this solution unless you really know what
you're doing and why you're doing it.

For the record, I did use this last solution twice in seven years. And
it was mostly to avoid repeating boilerplate code in some low-level
module of a framework. Definitively not a common case...
[1] which, FWIW, doesn't make much differences since attributes are
stored in dicts....
Lee
Aug 10 '07 #6
On 10 ago, 00:11, Lee Sander <lesa...@gmail.comwrote:
Hi,

I would like to define a new variable which is not predefined by me.
For example,
I want to create an array called "X%s" where "%s" is to be determined
based on the data I am processing. So, for example, if I the file
I'm reading has
g 99
on the first line, I want to create a new variable called "Xg" whose
length
is 99.
I tried eval("Xg=[0]*99") but that did not work.
>>letter = 'g'
import __main__
setattr(__main__,'X%s'%letter,[0]*99)
Xg
[0, 0, 0 ...

Anyway, I donīt see the point in this. Why donīt you just use
something like X['g'] instead?

Aug 10 '07 #7
Anyway, I donīt see the point in this. Why donīt you just use
something like X['g'] instead?
While it's not what the original author is intending, it seems to me
that dynamically adding fields could be useful when something like a
database schema changed frequently. For example, a row in a database
might contain data related to a customer and I would think it might be
useful to do something like this (where data is a dictionary created
from the column names and row data):

class Customer:
def __init__(self, data):
for col_name, value in data.iteritems():
setattr(self, col_name, value)

Alternatively, you could just assign the original dictionary to a
field on Customer called data and then access it by key:

class Customer:
def __init__(self, data):
self.customerData = data

I'm pretty new to Python so it's entirely possible I'm doing things
the hard way but if you had behavior on a class, I can see where
dynamically creating fields on it would be useful.
Aug 13 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Shaul Feldman | last post: by
1 post views Thread by BobAchgill | last post: by
reply views Thread by Peter Afonin | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.