Separate namespace from file hierarchy?

P

Peng Yu

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()
}
 
D

Diez B. Roggisch

Peng said:
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 define

dirname.both.__init__.py

and in that

from A import A
from B import B

Then you have your namespace.

Diez
 
Y

yuky

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()
 
B

Bruno Desthuilliers

yuky a écrit :
(snip)
Or there is other variant:

import dirname.A as dirname
a = dirname.A()

I wouldn't recommand this "variant", which is highly confusing.
 
S

Steven D'Aprano

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()


Have you tried this, or are you just assuming it will work?

You can't specify an arbitrary directory location for a module. For the
above to work, "dirname" has to be a package, which means it needs a
__init__.py file.

I'll assume from this point on that "dirname" is the name of a package.

####################

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.

Saying it is redundant three times is redundant.

If that worries you, then don't do it. You don't *have* to put the class
A in a module A.py, you can call the module anything appropriate:

import dirname.internal
a = dirname.internal.A()

You can arrange your package in whatever way seems sensible to you. What
I tend to do is something like this:

dirname/
+-- __init__.py
+-- A.py
+-- B.py


and then in __init__.py I have this:


from A import A
from B import B


so the caller can do this:

import dirname
a = dirname.A()
b = dirname.B()


without worrying about the internal structure of the package (the
submodules A and B).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top