class GeometricObject:
def __init__(self,color = 'green',filled='true'):
self.__color=color
self.__filled=filled
def getColor(self):
return self.__color
def setColor(self,color):
self.__color=color
def isFilled(self):
return self.__filled
def setFilled(self,filled):
self.__filled=filled
def __str__(self):
return "Color: " + self.__color + " Filled: " + str(self.__filled)
And this is the Triangle class:
from GeometricObject import GeometricObject
class Triangle(GeometricObject):
def __int__(self,side1=1.0,side2=1.0,side3=1.0):
super().__init__()
self.__side1=side1
self.__side2=side2
self.__side3=side3
def getPerimeter(self):
return self.__side1 + self.__side2 + self.__side3
def __str__(self):
return super().__str__()+" side 1: "+str(self.__side1)+" side 2: "+str(self.__side2)+" side 3: "+str(self.__side3)
from triangle import Triangle
from GeometricObject import GeometricObject
def main():
side1=int(input("Enter first side: "))
side2=int(input("Enter second side: "))
side3=int(input("Enter third side: "))
t1=Triangle(side1,side2,side3)
print(t1.getColor())
print(t1.getPerimeter())
print(t1.__str__())
main()
The error is occuring when I create the triangle object t1 in the main function: init() takes from 1 to 3 positional arguments but 4 were given...
I know there is other posts on similar errors like this but a lot of them were if you provided not enough arguments and the one's that did provide too many arguments did not answer my question.
You need to change __int__
to __init__
, and only pass super()
self
.
class Triangle(GeometricObject):
def __init__(self,side1=1.0,side2=1.0,side3=1.0):
#The correct alternative to below is:
#super(GeometricObject, self).__init__()
GeometricObject.__init__(self)
self.__side1=side1
self.__side2=side2
self.__side3=side3
To answer your second question, here's an example:
class A(object):
def __init__(self, color = 'blue'):
self.color = color
def __str__(self):
return str(self.color)
class B(A):
def __init__(self, name = 'steve'):
A.__init__(self)
self.name = name
def __str__(self):
#alternatively: return super(B, self).__str__() + " " + str(self.name)
return A.__str__(self) + " " + str(self.name)
prints "blue steve"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments