programmatically define a new variable on the fly

L

Lee Sander

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
 
D

Dustan

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.
 
D

Dustan

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:
 
I

Ian Clark

Lee said:
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
Traceback (most recent call last):
File said:
>>> 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
 
R

Roberto Bonvallet

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.
 
B

Bruno Desthuilliers

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....
 
B

Bart Ogryczak

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?
 
O

osiris43

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top