Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Help
#11
class SuperClass:
    def __init__(self,a1,a2):
        self.attribute1=a1
        self.attribute2=a2
        def getAttribute1(self):
            return self.attritube1
        def setAttribute2(self,a1):
            self.attritube1=a1
        def getAttribute2(self):
            return self.attribute2
        def setAttribute1(self,a2):
            self.attribute2=a2
class SubClass (SuperClass):
    def __init__(self,a1,a2,a3):
       SuperClass.__init__(self,a1,a2)
    def getAttribute3(self):
        return self.attribute3
    def setAttribute3(self,a3):
        self.attribute3=a3
    def printMessage(self):
        print("My attributes are:", self.getAttribute1() ,self.getAttribute2(), self.getAttribute3())
    
    testObject=SubClass("Hello","World","!!!")
    testΟbject.printMessage()
yes i correct the underscores and delete testobject. I cant find the problem in Superclass seem ok to me
Reply
#12
Again, look at the indentation of different methods in both classes. Do you think the indentation of getters and setters in the SuperClass is OK?

by the way, using super() in the SubClass.__init__() method was better, than current one, although current one is also OK.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
class SuperClass:
    def __init__(self,a1,a2):
        self.attribute1=a1
        self.attribute2=a2
        def getAttribute1(self):
            return self.attritube1
        def setAttribute2(self,a1):
            self.attritube1=a1
        def getAttribute2(self):
            return self.attribute2
        def setAttribute1(self,a2):
            self.attribute2=a2
class SubClass (SuperClass):
    def __init__(self,a1,a2,a3):
       SuperClass.__init__(self,a1,a2)
    def getAttribute3(self):
        return self.attribute3
    def setAttribute3(self,a3):
        self.attribute3=a3
    def printMessage(self):
        print("My attributes are:", self.getAttribute1() ,self.getAttribute2(), self.getAttribute3())
    
    testObject=SubClass("Hello","World","!!!")
    testΟbject.printMessage()
I guess i need __ after . but still i cant make it correct
Reply
#14
class SuperClass:
    def __init__(self,a1,a2):
        self.attribute1=a1
        self.attribute2=a2
    def getAttribute1(self):
        return self.attribute1
    def setAttribute2(self,a1):
        self.attritube1=a1
    def getAttribute2(self):
        return self.attribute2
    def setAttribute1(self,a2):
        self.attribute2=a2
        
class SubClass (SuperClass):
    def __init__(self,a1,a2,a3):
        SuperClass.__init__(self,a1,a2)
        # better
        # super().__init__(self,a1,a2)
        self.attribute3 = a3
       
    def getAttribute3(self):
        return self.attribute3
    def setAttribute3(self,a3):
        self.attribute3=a3
    def printMessage(self):
        print("My attributes are:", self.getAttribute1() ,self.getAttribute2(), self.getAttribute3())
     
testObject=SubClass("Hello","World","!!!")
testObject.printMessage()
you may want also to check the other thread - https://python-forum.io/Thread-Class-exa...-the-error
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#15
indentation error in your program . and also function define problem. and function not calling
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020