1
The following is the correct class definition you want.
class SomeClass:
def __init__(self,value):
self.some_variable = value
def setVariable(self,value):
self.some_variable = value
def getVariable(self):
return self.some_variable
This will set the attribute on the instance
rather than on the class
object.
>>> a = SomeClass(5)
>>> a.some_variable # Just as expected
5
>>> b = SomeClass(10)
>>> b.some_variable # This is its own variable
10
>>> b.some_variable = 20 # This won't change 'a'
>>> a.some_variable # Hasn't changed
5
I might also want to note that getters
and setters
are often not want you want when writing python since you can do instance.some_variable = 5
without the need of a getter or setter.
Source:stackexchange.com