[Tutor] Class Inheritance -> NameError

Tim Johnson tim at johnsons-web.com
Thu Dec 1 04:56:32 CET 2005


My thanks to Christopher and Liam. I've revisited this
with the following:
class test:
    def __init__(self):
        self.s = ' there'
    def val(self,V):
        print '%s%s' % (V,self.s)
class sub(test):
    def __init__(self):
        pass
The following console session:
>>> T = mylib.test()
>>> S = mylib.sub()
>>> S.val('hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "mylib.py", line 1609, in val
    print '%s%s' % (V,self.s)
AttributeError: sub instance has no attribute 's'

Removing references to self.s as in
class test:
    def __init__(self):
        pass
    def val(self,V):
        print V
class sub(test):
    def __init__(self):
        pass
gives a console session without the AttributeError
----------------------------------------------------------
|  Do I understand that classes inherit methods, but not |
|  variable attributes?                                  |
----------------------------------------------------------

Thanks:
I've use inherited classes before but haven't tried anything
with variables.
tim

-- 
Tim Johnson <tim at johnsons-web.com>
      http://www.alaska-internet-solutions.com


More information about the Tutor mailing list