1

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!

5
  • 2
    "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." No, there isn't. You must use self Commented Jul 15, 2019 at 21:31
  • self isn't a band-aid solution. It is literally the only way to distinguish between the class attribute and a global variable with the same name Commented Jul 15, 2019 at 21:32
  • You just made height, width local variables to __init__ method. You should read how OOP works. Commented Jul 15, 2019 at 21:32
  • Yes, I understand that. "the only way to distinguish between the class attribute and a global variable with the same name" well my question is that, should I have a unique global variable (declared outside a method), how do I access it from within a method? For instance, how would I access the class-variables of width and height? THAT is my issue. Commented Jul 15, 2019 at 21:50
  • @GreyAycock yes, you already have the answer, you use self.my_var Commented Jul 15, 2019 at 22:17

2 Answers 2

3

height and width are classic variables which will only live in your __init__ method, to solve that, you have to use self.height and self.width instead. self points to the current object and this is the way to bind something to it.

EDIT: You should read some articles about Python OOP, there a few things unique to this language. For example, a Python class has 1 constructor __new__ and 1 initializer __init__. The 1st one is really a constructor because the instance does not exist yet when you go into it, its return value is mostly the needed instance. Then Python automatically calls the __init__ method of the object returned by the constructor and it's where you build your instance attributes. Of course, you could bind attributes directly from __new__ but __init__ is here for that.

Sign up to request clarification or add additional context in comments.

9 Comments

lol beat me to it.
Well yes, that's what I've been doing, however I wanted to know if there was any other way of doing this. Surely not all Python programs are littered with countless "self"s, right?
Yes they are, unlike languages like C# where they're not enforced
Also, what then is the point of having the height and width variables declared outside of any methods? Do they just serve as final ints?
I edited my answer to add some explanations
|
1

As Shizzen83 said, self refers to the instance of the class object. So any variable that's going to be used throughout the class needs to start with 'self.'

def __init__(self, heightInput, widthInput, pixelValue):
    self.height = heightInput
    self.width = widthInput

def readHeight:
    print(self.height)

That's what you're looking for.

2 Comments

Adding explanation to code makes answer more useful.
Thanks for the reminder, edited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.