naming objects from string

Jeremy Sanders jeremy+complangpython at jeremysanders.net
Thu Sep 21 04:58:04 EDT 2006


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. You don't want to be
messing around creating random variables in the local or global namespace.

For instance

myvals = {}
myvals['bob'] = {}
myvals['pete'] = {}
...
myvals['bob']['apple'] = (1,2,3,4)
myvals['bob']['orange'] = (2,3,4)
myvals['pete']['red'] = (4,5,6,7)

and so on

>>> myvals
{'pete': {'red': (4, 5, 6, 7)}, 'bob': {'orange': (2, 3, 4), 'apple': (1, 2,
3,4)}}

-- 
Jeremy Sanders
http://www.jeremysanders.net/



More information about the Python-list mailing list