Too many imports to use a business class library?

Diez B. Roggisch deets at nospam.web.de
Thu Jul 13 04:10:00 EDT 2006


Sanjay schrieb:
> Hi all,
> 
> I am new to python. Sorry if this is too novice question, but I could
> not find an answer yet.
> 
> While coding a business class library, I think it is preferable to have
> one class per source file, rather than combining all classes into one
> file, considering multiple developers developing the classes.
> 
> So, as per my study of python, a developer using the business class
> library has to write so many imports, one per class. Like:
> 
> from person import Person
> from contact import Contact
> .
> .
> .
> 
> Is there not a simple solution, a single import and you are able to use
> all the classes? Is there anything wrong in my approcah? Waiting for
> suggestions.

While there is nothing technically wrong, in python usually several 
related classes (and functions!) are grouped in one module. Not one 
class per file.

Additionally, you can use one module to import others into its 
namespace. The result is that this allows to only have one import, but 
get lots of modules imported with that.

foo/__init__.py:
import bar
from baz import *

foo/bar.py:
class Bar(object):
     pass

foo/baz.py:
class Baz(object):
     pass


can then be used as this:


import foo

foo.bar.Bar()
foo.bar.Baz()


Diez



More information about the Python-list mailing list