Home > database >  what did i do wrong missing 1 required positional argument: 'self'
what did i do wrong missing 1 required positional argument: 'self'

Time:10-10

class Area(object):
    def __init__(self, base, height):
        self.base = base
        self.height = height

    def calculation(self):
        return(self.base * self.height)

area = Area(15, 2)
print(Area.calculation())

CodePudding user response:

The Area in your print() is capitalized, while your variable area is not. That means that you are calling the Area Object instead of the variable.
This should work :

class Area(object):
    def __init__(self, base, height):
        self.base = base
        self.height = height

    def calculation(self):
        return(self.base * self.height)

area = Area(15, 2)
print(area.calculation())

CodePudding user response:

You are calling the class object's calculation method (Area.calculation), instead of calling the calculation method of the Area instance (area.calculation). This works:

class Area(object):
    def __init__(self, base, height):
        self.base = base
        self.height = height

    def calculation(self):
        return self.base * self.height

area = Area(15, 2)
print(area.calculation())

Note the removed parentheses around the return argument - they are unnecessary as return is a keyword, not a function.

  • Related