Having problem with subclass
clp2 at rebertia.com
Fri Oct 1 04:08:33 CEST 2010
On Thu, Sep 30, 2010 at 6:54 PM, tekion <tekion at gmail.com> wrote:
> 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)").
More information about the Python-list