[Tutor] Importing classes when needed

Alan Gauld alan.gauld at btinternet.com
Mon May 30 09:25:51 CEST 2011


"Timo" <timomlists at gmail.com> wrote

> When the user clicks a button, I want to show all available parsers 
> and use the choosen one when the user clicks "ok".
> Is it ok to place the following code in the __init__.py?
> from parser1 import Parser1
> from parser2 import Parser2
> def get_parsers():
>     return [Parser1(), Parser2()]

Why not just return the classes?

return [Parser1,Parser2]?

Rather than instantiating them?

But this means having to maintain the list in init.py.
Why not use the os functions to read the file names
dynamically and build the list that way? Provided
the files use a standard naming scheme you don't
need to change the init code.

Just a thought (before coffeee which is always
dangerous!)


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




More information about the Tutor mailing list