By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,428 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

Too many imports to use a business class library?

P: n/a
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.

Thanks in advance.
Sanjay

Jul 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Sanjay wrote:
I am new to python. Sorry if this is too novice question,
Don't worry and welcome.

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.
The use of one class = one sourcefile is often coming from Java.
Usually in Python people use something in the middle, putting related
names / functions / classes in the same module.
And you can put related modules in the same directory (with the
__init__ file).

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.
Other things that you may find useful to organize your names and
modules:
- the __all__ list to choose what to export;
- you can import a whole directory of modules;
- you can put many names in the same import line (from xx import xy,
yz, etc).
- Sometimes you can even use from XX import * but this can be a little
dangerous.

Bye,
bearophile

Jul 13 '06 #2

P: n/a
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
Jul 13 '06 #3

P: n/a
Sanjay wrote:
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.
The "multiple developpers" already implies the use of a CVS, so it's a
dubious argument here IMHO. Also, Python is not Java, and doesn't force
you into using classes when other constructs will do (functions, dicts
etc). The organization of the source should be done with the
hi-cohesion/low-coupling rule in mind, ie symbols that are very closely
related should go in a same module.
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
.
As you already noticed, this sucks.
Is there not a simple solution,
- use a CVS (not necessarily 'the' CVS - subversion is somewhat better,
and there are other options too) to address the multi-developper problem
- put closely related symbols in a same module
- put closely related modules in a same package
a single import and you are able to use
all the classes?
Python as packages:
http://docs.python.org/tut/node8.htm...00000000000000
Is there anything wrong in my approcah?
cf above

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 13 '06 #4

P: n/a
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

Jul 13 '06 #5

P: n/a
Thanks a lot to all for the to-the-point info, quite vital to me...

Sanjay

Jul 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.