Relatively new Python programmer here. So I am trying to create a variable that is accessible and changeable by any method within a class that is initialized within the constructor. So far, I cannot figure out how to do this.
As sort of a "band-aid" to the problem, I've been using 'self' before every variable in the class. Surely, there is another way.
For instance:
class Image:
height, width = 0,0
def __init__(self, heightInput, widthInput, pixelValue):
height = heightInput
width = widthInput
def readHeight:
print(height)
testObj = Image(10, 10, 20)
testObj.readHeight()
I would expect the output to be "10", yet instead an error is thrown that 'height' is not a variable. I have many more questions similar to this, however in essence I would like to know if there is any way I can avoid using the "self" word so much. Thanks!