Having problem with subclass

Tim Chase python.list at tim.thechases.com
Thu Sep 30 22:14:16 EDT 2010


On 09/30/10 20:54, tekion wrote:
> 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





More information about the Python-list mailing list