My question is why I can't make a object of the class giving it an integer as an argument. It totally ignores the value that I pass as an argument to it. Yet, if I call the area function from the class and pass the same number into it's parentheses it will output the desired result of 144.
My best guess from what I have read so far is that the way the classes function is coded is incorrect for what I am hoping that it will achieve. Do I need to have the area function take it's argument like:
# -*- coding: utf-8 -*-
sides = 4
def __init__(self, length):
self.length = length
def area(self, length):
return length * length
box = square(12)
TypeError: area() missing 1 required positional argument: 'length'
output: <bound method square_shape.area of <__main__.square_shape object at 0x7f5f88355b70>>
You are thinking of self.length. length (without a "self." in front of it) is just a local variable. If it's an argument, you need to provide it.
What you probably want is:
return self.length * self.length