Python Forum
Class example -i don't understand the error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class example -i don't understand the error
#1
class SuperClass:
    def __init__(self, a1, a2):
        self.attribute1 = a1
        self.attribute2 = a2
    def getAttribute1(self):
        return self.attribute1
    def setAttribute1(self, a1):
        self.attribute1 = a1
    def getAttribute2(self):
        return self.attribute2
    def setAttribute1(self, a2):
        self.attribute2 = a2

class SubClass(SuperClass):
    def getAttribute3(self):
        return self.attribute3
    def setAttribute3(self, a3):
        elf.attribute3 = a3
    def printMessage(self):
        print("My attributes are: ", self.getAttribute1(), \
        self.getAttribute2(), self.getAttribute3())

testObject = SubClass("i", "am", "awesome!")
testObject.printMessage()
and when i run it i get this:
Error:
Traceback (most recent call last): File "/home/main.py", line 23, in <module> testObject = SubClass("Hello", "world", "!!!") TypeError: __init__() takes 3 positional arguments but 4 were given
what is that about?
Reply
#2
def __init__(self, a1, a2):
You need to supply a1 and a2, but are sending three items ("i", "am", "awesome!") instead of two.
Reply
#3
When you call SubClass(), that calls the class's __init__ method. Since __init__ is not defined for SubClass, it inherits the one from its parent, SuperClass. The signature for that method is def __init__(self, a1, a2):. The self parameter is automatically supplied by Python, so you only have two parameters left: a1 and a2. You gave three parameters, Python supplied self automatically, so that's four parameters for a method which only has three defined.

For more on classes, check out this tutorial.

BTW, where did you get that code from? It's horrible Python code. You never almost never use getters and setters in Python. They slow things down, make your code harder to understand, and don't actually do anything. If you really need that sort of security (as in, self.y must change whenever self.x changes) you use properties. But generally you just don't bother.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Feb-01-2019, 04:34 PM)ichabod801 Wrote: When you call SubClass(), that calls the class's __init__ method. Since __init__ is not defined for SubClass, it inherits the one from its parent, SuperClass. The signature for that method is def __init__(self, a1, a2):. The self parameter is automatically supplied by Python, so you only have two parameters left: a1 and a2. You gave three parameters, Python supplied self automatically, so that's four parameters for a method which only has three defined.

For more on classes, check out this tutorial.

BTW, where did you get that code from? It's horrible Python code. You never almost never use getters and setters in Python. They slow things down, make your code harder to understand, and don't actually do anything. If you really need that sort of security (as in, self.y must change whenever self.x changes) you use properties. But generally you just don't bother.

it's an university exercise.I must find the mistakes and correct them and change the code (small changes) if it's necessary to run correctly and the final print should be "i am awesome"
Reply
#5
The professor must be a java programmer, as stated by Ichabod very poor example.
Since you say it's an assignment, the hints provided above should give you a clue so that you can finish the assignment.
trace out what a1 and a2's role are in the code.
There is also more than one way to answer, as you can add another attribute to SuperClass, or remove on from testObject
Reply
#6
(Feb-01-2019, 04:45 PM)Larz60+ Wrote: The professor must be a java programmer, as stated by Ichabod very poor example.
Since you say it's an assignment, the hints provided above should give you a clue so that you can finish the assignment.
trace out what a1 and a2's role are in the code.
There is also more than one way to answer, as you can add another attribute to SuperClass, or remove on from testObject
i followed Ichabod explanation - i thank him for the hint-and i added one more attribute to SuperClass.
As for the second solution can you explain it a bit more.I want to learn as much as i can.
Reply
#7
The other way would be to add a third argument to SuperClass, but it's dumb unless it's actually needed.
And of course you would also have to add handlers for for this argument.

The real lesson to learn here is that python doesn't need setters and getters, that's java
Reply
#8
(Feb-01-2019, 10:50 PM)Larz60+ Wrote: The real lesson to learn here is that python doesn't need setters and getters, that's java

so,in python how should this code be???
if there are no needs for setters and getters how can i write it??
Using functions??
Can you please show me?

--As for the exercise i just added a third attribute to Superclass--
Reply
#9
As to the original question - I think the idea is to have __init__() in the child class that will override the parent/super class __init__(). You will need also to initialize the super class using super(). Otherwise it doesn't make sense to have a child class that inherits. And it's clear "the teacher" is using getters and setters and for attribute3 these are in the child class, not in parent class. If you add a3 to parent class, I guess you would be expected to add also the getters and setters and then it does not make sense to have them also in the child class. So what I would do for the original question:

class SuperClass:
    def __init__(self, a1, a2):
        self.attribute1 = a1
        self.attribute2 = a2
    def getAttribute1(self):
        return self.attribute1
    def setAttribute1(self, a1):
        self.attribute1 = a1
    def getAttribute2(self):
        return self.attribute2
    def setAttribute1(self, a2):
        self.attribute2 = a2
 
class SubClass(SuperClass):
    def __init__(self, a1, a2, a3):
        super(SubClass, self).__init__(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("i", "am", "awesome!")
testObject.printMessage()
Output:
My attributes are: i am awesome!
(Feb-02-2019, 06:50 AM)sonedap Wrote: so,in python how should this code be???

class SuperClass:
    def __init__(self, a1, a2):
        self.attribute1 = a1
        self.attribute2 = a2
 
class SubClass(SuperClass):
    def __init__(self, a1, a2, a3):
        super(SubClass, self).__init__(a1, a2)
        self.attribute3 = a3

    def print_message(self):
        print("My attributes are: ", self.attribute1, self.attribute2, self.attribute3)
        # actually I would use str.format() or f-strings
        # print(f"My attributes are: {self.attribute1} {self.attribute2} {self.attribute3}")
 
test_object = SubClass("i", "am", "awesome!")
test_object.print_message()
Of course there might be cases when you need getters and setters, but in most cases you probably don'y need them
a read that you may find interesting: Python Is Not Java

Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the 'property' built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in 'property'. That means that until you prove that you need anything more than a simple attribute access, don't write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.

In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters.
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
#10
(Feb-02-2019, 08:01 AM)buran Wrote: As to the original question - I think the idea is to have __init__() in the child class that will override the parent/super class __init__(). You will need also to initialize the super class using super(). Otherwise it doesn't make sense to have a child class that inherits. And it's clear "the teacher" is using getters and setters and for attribute3 these are in the child class, not in parent class. If you add a3 to parent class, I guess you would be expected to add also the getters and setters and then it does not make sense to have them also in the child class. So what I would do for the original question:

class SuperClass:
    def __init__(self, a1, a2):
        self.attribute1 = a1
        self.attribute2 = a2
    def getAttribute1(self):
        return self.attribute1
    def setAttribute1(self, a1):
        self.attribute1 = a1
    def getAttribute2(self):
        return self.attribute2
    def setAttribute1(self, a2):
        self.attribute2 = a2
 
class SubClass(SuperClass):
    def __init__(self, a1, a2, a3):
        super(SubClass, self).__init__(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("i", "am", "awesome!")
testObject.printMessage()
Output:
My attributes are: i am awesome!
(Feb-02-2019, 06:50 AM)sonedap Wrote: so,in python how should this code be???

class SuperClass:
    def __init__(self, a1, a2):
        self.attribute1 = a1
        self.attribute2 = a2
 
class SubClass(SuperClass):
    def __init__(self, a1, a2, a3):
        super(SubClass, self).__init__(a1, a2)
        self.attribute3 = a3

    def print_message(self):
        print("My attributes are: ", self.attribute1, self.attribute2, self.attribute3)
        # actually I would use str.format() or f-strings
        # print(f"My attributes are: {self.attribute1} {self.attribute2} {self.attribute3}")
 
test_object = SubClass("i", "am", "awesome!")
test_object.print_message()
Of course there might be cases when you need getters and setters, but in most cases you probably don'y need them
a read that you may find interesting: Python Is Not Java

Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the 'property' built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in 'property'. That means that until you prove that you need anything more than a simple attribute access, don't write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.

In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters.

thanks for the link.i am gonna study it soon enough
Reply


Forum Jump:

User Panel Messages

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