Search code examples
pythonself

Is declaring [almost] everything with self. alright (Python)?


I have a habit to declare new variables with self. in front to make it available to all methods. This is because sometimes I thought I don't need the variable in other methods. But halfway through I realized that I need it to be accessible in other methods. Then I have to add self. in front of all that variable.

So my question is, besides needing to type 5 characters more each time I use a variable, are there any other disadvantages? Or, how do you overcome my problem?


Solution

  • Set a property on self only when the value is part of the overall object state. If it's only part of the method state, then it should be method-local, and should not be a property of self.