Implementing Java-style getters and settersΒΆ
Python is not Java. If you need to set or get the members of a class or object, just expose the member publicly and access it directly. If you need to perform some computations before getting or setting the member, then use Pythonβs built-in property decorator.
Anti-patternΒΆ
The programmer below comes to Python from a long career as a Java programmer. For every class member that he wants to expose publicly, he defines a get and set method for that member. This is common practice in Java, but is frowned upon in Python as a waste of time and a cause of unnecessary code.
class Square(object):
def __init__(self, length):
self._length = length
# Java-style
def get_length(self):
return self._length
# Java-style
def set_length(self, length):
self._length = length
r = Square(5)
r.get_length()
r.set_length(6)
Best practiceΒΆ
Access the members directlyΒΆ
In Python it is acceptable to simply access class or object members directly. The modified code below exposes the length member as a public member. This is signified by the fact that there is no underscore character at the beginning of the member name. The get_length() and set_length() methods are no longer necessary so they have been deleted.
class Square(object):
def __init__(self, length):
self.length = length
r = Square(5)
r.length
r.length = 6
Use built-in property decoratorΒΆ
When a member needs to be slightly protected and cannot be simply exposed as a public member, use Pythonβs property decorator to accomplish the functionality of getters and setters.
class Square(object):
def __init__(self, length):
self._length = length
@property
def length(self):
return self._length
@length.setter
def length(self, value):
self._length = value
@length.deleter
def length(self):
del self._length
r = Square(5)
r.length # automatically calls getter
r.length = 6 # automatically calls setter