Skip to content

Commit 736660a

Browse files
committed
Create implementing_java-style_getters_and_setters.rst.
1 parent d44ed4e commit 736660a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
Implementing Java-style getters and setters
2+
===========================================
3+
4+
Summary
5+
-------
6+
7+
Use the built-in function decorator ``property`` to implement getters and setters on Python classes and objects, or just access the members directly. Don't use the Java style of defining ``get_x`` and ``set_x`` methods for every public member of a class.
8+
9+
Description
10+
-----------
11+
12+
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.
13+
14+
Examples
15+
----------
16+
17+
Implementing Java-style getters and setters
18+
...........................................
19+
20+
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 unnecessary code.
21+
22+
.. warning:: The code below is an example of an error. Using this code will create bugs in your programs!
23+
24+
.. code:: python
25+
26+
class Square(object):
27+
def __init__(length):
28+
self._length = length
29+
def get_length(self): # Java-style
30+
return self._length
31+
def set_length(self, length): # Java-style
32+
self._length = length
33+
34+
r = Square(5)
35+
r.get_length()
36+
r.set_length(6)
37+
38+
Solutions
39+
---------
40+
41+
Access the members directly
42+
...........................
43+
44+
In Python it is acceptable to simply access class or object members directly. The modified module 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.
45+
46+
.. code:: python
47+
48+
class Square(object):
49+
def __init__(length):
50+
self.length = length
51+
52+
r = Square(5)
53+
r.length
54+
r.length = 6
55+
56+
Use built-in ``property`` decorator
57+
...................................
58+
59+
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.
60+
61+
.. code:: python
62+
63+
class Square(object):
64+
def __init__(self, length):
65+
self._length = length
66+
67+
@property
68+
def length(self):
69+
return self._length
70+
71+
@width.setter
72+
def length(self, value):
73+
self._length = value
74+
75+
@width.deleter
76+
def length(self):
77+
del self._length
78+
79+
r = Square(5)
80+
r.length # automatically calls getter
81+
r.length = 6 # automatically calls setter
82+
83+
References
84+
----------
85+
- `Python Built-in Functions - property <https://docs.python.org/2/library/functions.html#property>`_
86+
- `dirtSimple - Python Is Not Java <http://dirtsimple.org/2004/12/python-is-not-java.html>`_
87+
- `Stack Overflow - What's the Pythonic Way to use getters and setters? <http://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters>`_

0 commit comments

Comments
 (0)