@@ -176,8 +176,8 @@ repetition of `very.deep.module`.
176
176
Object-oriented programming
177
177
---------------------------
178
178
179
- Python is sometime described as an object-oriented programming language. This
180
- can be somewhat misleading and need to be clarified.
179
+ Python is sometimes described as an object-oriented programming language. This
180
+ can be somewhat misleading and needs to be clarified.
181
181
182
182
In Python, everything is an object, and can be handled as such. This is what is
183
183
meant when we say that, for example, functions are first-class objects.
@@ -188,46 +188,46 @@ object-oriented language.
188
188
189
189
However, unlike Java, Python do not impose object-oriented programming as the
190
190
main programming paradigm. It is perfectly viable for a Python project to not
191
- be object-oriented, ie . to use no or very few class definitions, class
192
- inheritance, and any other mechanism that are specific to object-oriented
191
+ be object-oriented, i.e . to use no or very few class definitions, class
192
+ inheritance, or any other mechanisms that are specific to object-oriented
193
193
programming.
194
194
195
195
Moreover, as seen in the modules _ section, the way Python handles modules and
196
- namespaces gives directly to the developer a natural way to ensure
196
+ namespaces gives the developer a natural way to ensure
197
197
encapsulation and separation of abstraction layers, both being the most common
198
198
reasons to use object-orientation. Therefore, Python programmers have more
199
199
latitude to not use object-orientation, when it is not required by the business
200
- model to be constructed .
200
+ model.
201
201
202
- There are some reasons to avoid unnecessary object-orientation. Definining
202
+ There are some reasons to avoid unnecessary object-orientation. Defining
203
203
custom classes is useful when we want to glue together some state and some
204
- functionality. The problem, as pointed out by the discussions about functional
204
+ functionality. The problem, as pointed out by the discussions about functional
205
205
programming, comes from the "state" part of the equation.
206
206
207
- In some architectures, typically web applications, instances of Python
208
- processes are spawned simultaneously to answer to external requests that can
209
- happen at the same time. In this case, holding some state into instanciated
207
+ In some architectures, typically web applications, multiple instances of Python
208
+ processes are spawned to respond to external requests that can
209
+ happen at the same time. In this case, holding some state into instantiated
210
210
objects, which means keeping some static information about the world, is prone
211
- to concurrency problems or race-conditions: between the initialization of the
211
+ to concurrency problems or race-conditions. Sometime between the initialization of the
212
212
state of an object, usually done with the __init__() method, and the actual use
213
- of the object state through one of its method , the world may have changed, and
213
+ of the object state through one of its methods , the world may have changed, and
214
214
the retained state may be outdated. For example, a request may load an item in
215
215
memory and mark it as read by a user. If another request requires the deletion
216
- of this item at the same, it may happen that the deletion actually occur after
216
+ of this item at the same, it may happen that the deletion actually occurs after
217
217
the first process loaded the item, and then we have to mark as read a deleted
218
218
object.
219
219
220
220
This and other issues led to the idea that using stateless functions is a
221
221
better programming paradigm.
222
222
223
- Another way to say the same thing is to propose to use functions and procedures
224
- with as few implicit context and side-effects as possible. A function's
225
- implicit context is decelable when the function body refers to some global
226
- variables or fetches data from the persistence layer . Side-effects are the
227
- opposite: if a function body modifies the global context or save or delete data
228
- on the persistence layer, it is said to have side-effect.
223
+ Another way to say the same thing is to suggest using functions and procedures
224
+ with as few implicit contexts and side-effects as possible. A function's
225
+ implicit context is made up of any of the global variables or items in the persistence layer
226
+ that are accessed from within the function . Side-effects are the changes that a function makes
227
+ to it's implicit context. If a function saves or deletes data in a global variable or
228
+ in the persistence layer, it is said to have a side-effect.
229
229
230
- Isolating carefully functions with context and side-effects from functions with
230
+ Carefully isolating functions with context and side-effects from functions with
231
231
logic (called pure functions) allow the following benefits:
232
232
233
233
- Pure functions are more likely to be deterministic: given a fixed input,
@@ -239,7 +239,7 @@ logic (called pure functions) allow the following benefits:
239
239
- Pure functions are easier to test with unit-tests: There is less
240
240
need for complex context setup and data cleaning afterwards.
241
241
242
- - Pure functions are easier to manipulate, decorate _, pass-around.
242
+ - Pure functions are easier to manipulate, decorate _, and pass-around.
243
243
244
244
In summary, pure functions, without any context or side-effects, are more
245
245
efficient building blocks than classes and objects for some architectures.
0 commit comments