[Tutor] Instantiating a list of strings into a list of classes

Alan Gauld alan.gauld at btinternet.com
Sun Mar 7 00:57:02 CET 2010


"Daryl V" <vandyke.geospatial at gmail.com> wrote

> What I want to do is use the first entry in that row (row[0]) as the
> variable name for the instantiated class.

Thats usually a very bad idea. Not least because all the code that
comes after it would somehow, magically, have to know about this
brand new variable that has appeared.

Its generally better to store these kinds of things in a list or 
dictionary.
You can use the name as the key into the dictionary if you like.

Then you can process the dictionary like any other collection of objects.
Or you can refer to specific ones using the name. And the previously
written code doen't get confused aboutt unexpected names appearing.

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




More information about the Tutor mailing list