[Tutor] user created lists

Alan Gauld alan.gauld at btinternet.com
Thu Apr 12 09:55:09 CEST 2012


On 12/04/12 08:25, Christian Witts wrote:

>> I recommend that you bite the bullet and use a dedicated dictionary or list
>> to hold your five lists from the very begining:
> You could use globals() then instead of var(),  although it's a
> do-it-at-your-own-risk situation then if you overwrite built-ins and
> other functions in the namespace.  Creating your own dictionary with the
> 'variable name' as the key, would be the nicer solution instead.

Trying to create variables on the fly is usually a bad idea, although it 
seems to appeal to beginners a lot. The problem is: how does the rest of 
the pre-existing code know about these new variable names so as to 
access them.

If you keep the new values in a collection they can be access via a 
loop. But randomly named variables in your code require all sorts of
extra effort to create the name and deference the namespace each time 
you want to access them.

Anonymous values are best kept in anonymous structures IMHO.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list