On Oct 20, 3:51 pm, Peng Yu <pengyu...@gmail.com> wrote: > Suppose I have the dirname/both.py, which has the definitions of > classes A and B. I can use this module in the following code. > > ################# > import dirname.both > > a=dirname.both.A() > b=dirname.both.B() > #################### > > When the definitions of A and B become too long, it is better that I > put them in two different files (to improve the maintainability), for > example, dirname/A.py and dirname/B.py. Now, the code becomes > > ################# > import dirname.A > import dirname.B > > a=dirname.A.A() #two A seems redundant > b=dirname.B.B() #two B seems redundant > #################### > > However, the last two lines are annoying to me, as 'A' and 'B' appears > twice, which seems redundant. > > In C++, I can define namespace independent of file hierarchy (as shown > in the following code). I'm wondering if there is a way to make the > namespace separate from the file hierarchy in python. > > #include <dirname/A.hpp> > #include <dirname/B.hpp> > > int main() { > dirname::A a = dirname::A() > dirname::B b = dirname::B() > > } > >
You can use: from dirname.A import A then you can use: a = A() Or there is other variant: import dirname.A as dirname a = dirname.A() -- http://mail.python.org/mailman/listinfo/python-list