Increment Variable Name
Terry Reedy
tjreedy at udel.edu
Thu Jan 24 17:34:05 EST 2008
"Paul Hankin" <paul.hankin at gmail.com> wrote in message
news:fdff5a3c-376c-41da-bc7c-| > Do you want to do this?:
| > locals()['var'+str(1)] = "spam"
|
| As I recently learnt in this newsgroup, that's not guaranteed to work.
| >From http://docs.python.org/lib/built-in-funcs.html
|
| Warning: The contents of this dictionary should not be modified;
| changes may not affect the values of local variables used by the
| interpreter.
It currently works at module level, where locals() is globals(). But then,
one might as well write globals()['var'+str(1)] = "spam". But in Python,
one is almost certainly better to use a collection object than this idiom
(indefinite multiple numbered variables) imported from languages which do
not have them.
More information about the Python-list
mailing list