HELP: Creating dynmaic variable names

Alex Martelli aleax at aleax.it
Thu Mar 6 09:55:58 CET 2003


<posted & mailed>

Frank Schwaak wrote:

> Hi,
> 
> i need some help for creating dynamic variable names for lists.

You WANT to do that, but you really don't NEED to.  It would be
far, far better to avoid this approach in favour of others.


> Example:
> 
> i have the following text:
> 
> ENTRY = TestA
> Position : 11
> Info: Information A
> 
> ENTRY = TestB
> Position: 12
> Info: Information B
> 
> Now i want to get these into a list or hash named like this
> 
> hash1 = {ENTRY : 'TestA', Position : '11', Info : 'Information A'}
> hash2 = {ENTRY : 'TestB', Position : '12', Info : 'Information B'}

A list or hash would be either:

[ {ENTRY : 'TestA', Position : '11', Info : 'Information A'},
  {ENTRY : 'TestB', Position : '12', Info : 'Information B'}
]

(a list), or:

{ 'hash1': {ENTRY : 'TestA', Position : '11', Info : 'Information A'},
  'hash2': {ENTRY : 'TestB', Position : '12', Info : 'Information B'}
}

(a hash, known in Python as a dict or dictionary).  Either approach
would be *ENORMOUSLY* better than creating multiple separate variables,
which has only problems and no advantages whatsoever.


> So the aim is to create a new hash on the line between the two sections.
> The Problem is, that i don't know how many sections there are.

Therefore you should NOT create multiple separate variables, which
would only give you later the problem of finding out about them and
accessing them: you should group all sections into a container of
containers, be it a list or dictionary, so you can later loop over
it, access it easily, and so on.


> So, my thought was to do something like this
> 
> i = 0
> for i = 1 to xx
>    hash$i = .....
>    i = i + 1
> 
> Is there a way to do this in Python?

Yes, there is, although it's pointless and deleterious to do so.

But if you're really ADAMANT about writing a horrible program rather
than a good one, Python doesn't FORCE you to write decent code: Python
is about enabling you to write excellent code, not about forbidding
you to write utter disasters.

for i in range(N):
    onemoreentry = "compute it however you wish to"
    exec 'hash%s = onemoreentry' % i

Note that, if you do this in a function, you'll also manage to slow
that function down appreciably -- damaging your code's performance
as well as its clarity and maintaintability.

The sensible way to do it, with a container of containers, e.g.:

hashes = {}
for i in range(N):
    onemoreentry = "compute it however you wish to"
    hashes['hash%s' % i] = onemoreentry

or generally better:

hashes = {}
for i in range(N):
    onemoreentry = "compute it however you wish to"
    hashes.append(onemoreentry)

is going to be faster as well as clearer and more readable, not
to mention producing a structure that's certain to be more useful
in the following parts of your program.  But, the choice's up to you.


Alex






More information about the Python-list mailing list