1

I wrote the following code in python (to practice), but I can not figure out where the error is:

class BClass:
    def __init__ (self, message):
        self.message=message
    def printMessage(self):
        print(self.message)

class AClass(BClass):
    def __init__(self, message):
        super(). __init__(message)

m1=AClass("ciao")
m1.printMessage()

Can you help me? Thanks

Pydavide
  • 59
  • 4
  • What error are you getting? – KolaB Mar 26 '18 at 10:32
  • You have a syntactically erroneous space after the `.` in `super(). __init__(message)`. – Synook Mar 26 '18 at 10:33
  • @Synook When I run with python3 I get no error. When I run with python 2.7 I get *TypeError: super() takes at least 1 argument (0 given)* - even after removing the space. – KolaB Mar 26 '18 at 10:37
  • @Synook I fixed the code but it returns this error: `code` m1=AClass("ciao") Traceback (most recent call last): File "", line 1, in File "", line 3, in __init__ TypeError: super() takes at least 1 argument (0 given) `code` – Pydavide Mar 26 '18 at 10:37
  • Possible duplicate of [What does 'super' do in Python?](https://stackoverflow.com/questions/222877/what-does-super-do-in-python) – Synook Mar 26 '18 at 10:39

1 Answers1

1

As mentioned in comments this topic has been discussed before. I found this link cuts to the chase of your problem super in python2.7 However, here is a solution for your particular case that works for python2.7:

class BClass(object):
    message = ''
    def __init__ (self, message):
        self.message=message
    def printMessage(self):
        print(self.message)

class AClass(BClass):
    def __init__(self, message):
        super(AClass, self).__init__(message)

m1=AClass("ciao")
m1.printMessage()
KolaB
  • 501
  • 3
  • 11