accessing superclass methods from subclass

MRAB python at mrabarnett.plus.com
Sat May 8 20:13:18 EDT 2010


ben wrote:
> Why doesn't this work:
> 
> class C1:
>     def f1(self):
>         print("f1")
> 
> class C2(C1):
>     f1()
> 
> 
> It throws this error:
> 
> Traceback (most recent call last):
>   File "./c1.py", line 7, in <module>
>     class C2(C1):
>   File "./c1.py", line 8, in C2
>     f1()
> NameError: name 'f1' is not defined
> 
> 
> f1() is an attribute of class C1, C2 inherits C1, so why can't it see
> it?
> 
Name lookup works differently from some other languages. You need to be
explicit and tell it where 'f1' is defined:

     C1.f1()

However, that will raise a different exception. I don't know what you're
trying to do.



More information about the Python-list mailing list