looking for way to include many times some .py code from anotherpython code

Brian Roberts brian at mirror.org
Tue Mar 8 19:10:52 EST 2005


> 
> Am I so deperately fighting the language? No-one here on the list needs to set hundreds
> variables at once somewhere in their code? I still don't get why:
> 

I once (and only once) needed hundreds of variables in a program.  It
was to simplify creation of unit tests, not for production use.  The
variable names and data (representing a graph with named nodes) was
stored in a text file, I read that file and used setattr() to create
each variable.  This was in a module that did nothing else, and was
imported by unit test code that benefited from the names when setting
up easily readable test cases.

Background if you're new to Python: importing a module *executes* it;
for most modules the only important stuff executing is the class and
def statements.  However, you can execute more stuff when (rarely)
necessary -- reading a file in my case.  This is very different than,
say, C or C++, which has separate include and execute steps.

In general, you want to either use the built-in lists and dicts, or
create classes/objects to represent hundreds of things.

Brian.



More information about the Python-list mailing list