I was wondering if there is a preferred method to refer to a value from within a class.
For example in the following code:
class Coordinate(object):
def __init__(self, x):
self.x = x
def getX(self):
return(self.x)
def transform(self, dx):
return self.x - dx
When I want to use x
somewhere in another method of the Coordinate
class, for example in the transform
method, what is the preferred way to refer to x
? Is it with self.x
or self.getX()
, such that transform
becomes:
def transform(self, dx):
return self.getX() - dx
I understood that using getX() is preferred when trying to get the value x
from outside the class. Thus:
>>> c = Coordinate(13)
>>> print(c.getX()) # Preferred method
13
>>> print(c.x)
13