Too many imports to use a business class library?

Peter Otten __peter__ at web.de
Thu Jul 13 04:19:29 EDT 2006


Sanjay wrote:

> While coding a business class library, I think it is preferable to have
> one class per source file, 

... but I don't like the consequences :-)

> 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.

Put person.py, contact.py etc into a subdirectory (which I assume is called
business). Then create a file business/__init__.py to turn the directory
into a package

#contents of business/__init__.py
from person import Person
from contact import Contact

You can then use your library like so:

from business import Person, Contact

person = Person(...)
contact = Contact(...)
 
or preferably:

import business

person = business.Person(...)
contact = business.Contact(...)

Peter




More information about the Python-list mailing list