Having problem with subclass

T

tekion

All,
I have file name A.py with a class name Aclass. I have a file name
B.py with sub class of Aclass, like
import A
class Bclass(Aclass)
....rest of code
When I test it, calling B.py, I am getting:

class Bclas(Aclass):
NameError: name 'Aclass' is not defined

When I move Bclass to A.py, it works. Is there some restriction in
python that sub classing a class has be in the same file as the class
you're are sub classing? Thanks.
 
C

Chris Rebert

All,
I have file name A.py with a class name Aclass. I have a file name
B.py with sub class of Aclass, like
import A
class Bclass(Aclass)
...rest of code
When I test it, calling B.py, I am getting:

class Bclas(Aclass):
NameError: name 'Aclass' is not defined

When I move  Bclass to A.py, it works.  Is there some restriction in
python that sub classing a class has be in the same file as the class
you're are sub classing? Thanks.

"import A" *only* imports the name "A" into B's namespace; names
within A.py are not directly imported and can only be referenced using
the dot operator on A (i.e. "A.Aclass").
"from A import *" takes all the names defined in A.py and imports them
into B's namespace (which is apparently what you expected "import A"
to do), but this is frowned upon as it can pollute B's namespace.

You should probably either do "from A import Aclass" (i.e. explicitly
specifying which names from A you want), or instead refer to Aclass in
B.py using "A.Aclass" (i.e. "class Bclass(A.Aclass)").

Cheers,
Chris
 
T

Tim Chase

All,
I have file name A.py with a class name Aclass. I have a file name
B.py with sub class of Aclass, like
import A
class Bclass(Aclass)
...rest of code
When I test it, calling B.py, I am getting:

class Bclas(Aclass):
NameError: name 'Aclass' is not defined

When I move Bclass to A.py, it works. Is there some restriction in
python that sub classing a class has be in the same file as the class
you're are sub classing?

This is pretty straight-forward name-spacing. You can use

class Bclass(A.Aclass):
...

or

from A import Aclass
class Bclass(Aclass):
...


But in your code as it currently stands, the module "A" is the
only thing in scope, not "Aclass", so you have to either bring
Aclass into scope, or qualify it.

-tkc
 
S

Steven D'Aprano

When I move Bclass to A.py, it works. Is there some restriction in
python that sub classing a class has be in the same file as the class
you're are sub classing? Thanks.

Others have already solved your main problem (you need to refer to
A.Aclass rather than just Aclass), but I'd like to make another comment.

Unlike Java, Python programmers rarely see the need to have one class per
file, especially if they are small classes. Generally subclasses will be
small -- you might only change a handful of methods. I'd even go say as
to say that the Java (anti-)pattern of placing each and every class into
its own file is actively frowned upon by Python programmers.

By all means arrange your classes into separate modules if that is the
most natural way to arrange them, but don't artificially break apart
related classes into separate modules.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top