@@ -172,6 +172,83 @@ Lastly, a convenient syntax is available for importing deeply nested packages:
172172`import very.deep.module as mod ` allow to use `mod ` in place of the verbose
173173repetition of `very.deep.module ` in front of each calls to module items.
174174
175+ Object-oriented programming
176+ ---------------------------
177+
178+ Python is sometime described as an object-oriented programming language. This
179+ can be somewhat misleading and need to be clarified.
180+
181+ In Python, everything is an object, and can be handled as such. This is what is
182+ meant when we say that, for example, functions are first-class objects.
183+ Functions, classes, strings, and even types are objects in Python: like any
184+ objects, they have a type, they can be passed as function arguments, they may
185+ have methods and properties. In this understanding, Python is an
186+ object-oriented language.
187+
188+ However, unlike Java, Python do not impose object-oriented programming as the
189+ main programming paradigm. It is perfectly viable for a Python project to not
190+ be object-oriented, ie. to use no or very few class definitions, class
191+ inheritance, and any other mechanism that are specific to object-oriented
192+ programming.
193+
194+ Moreover, as seen in the modules _ section, the way Python handles modules and
195+ namespaces gives directly to the developer a natural way to ensure
196+ encapsulation and separation of abstraction layers, both being the most common
197+ reasons to use object-orientation. Therefore, Python programmers have more
198+ latitude to not use object-orientation, when it is not required by the business
199+ model to be constructed.
200+
201+ There are some reasons to avoid unnecessary object-orientation. Definining
202+ custom classes is useful when we want to glue together some state and some
203+ functionality. The problem, as pointed out by the discussions about functional
204+ programming, comes from the "state" part of the equation.
205+
206+ In some architectures, typically web applications, instances of Python
207+ processes are spawned simultaneously to answer to external requests that can
208+ happen at the same time. In this case, holding some state into instanciated
209+ objects, which means keeping some static information about the world, is prone
210+ to concurrency problems or race-conditions: between the initialization of the
211+ state of an object, usually done with the __init__() method, and the actual use
212+ of the object state through one of its method, the world may have changed, and
213+ the retained state may be outdated. For example, a request may load an item in
214+ memory and mark it as read by a user. If another request requires the deletion
215+ of this item at the same, it may happen that the deletion actually occur after
216+ the first process loaded the item, and then we have to mark as read a deleted
217+ object.
218+
219+ This and other issues led to the idea that using stateless functions is a
220+ better programming paradigm.
221+
222+ Another way to say the same thing is to propose to use functions and procedures
223+ with as few implicit context and side-effects as possible. A function's
224+ implicit context is decelable when the function body refers to some global
225+ variables or fetches data from the persistence layer. Side-effects are the
226+ opposite: if a function body modifies the global context or save or delete data
227+ on the persistence layer, it is said to have side-effect.
228+
229+ Isolating carefully functions with context and side-effects from functions with
230+ logic (called pure functions) allow the following benefits:
231+
232+ - Pure functions are more likely to be deterministic: given a fixed input,
233+ the output will always be the same.
234+
235+ - Pure functions are much easier to change or replace if they need to
236+ be refactored or optimized.
237+
238+ - Pure functions are easier to test with unit-tests: There is less
239+ need for complex context setup and data cleaning afterwards.
240+
241+ - Pure functions are easier to manipulate, decorate _, pass-around.
242+
243+ In summary, pure functions, without any context or side-effects, are more
244+ efficient building blocks than classes and objects for some architectures.
245+
246+ Obviously, object-orientation is useful and even necessary in many cases, for
247+ example when developing graphical desktop applications or games, where the
248+ things that are manipulated (windows, buttons, avatars, vehicles) have a
249+ relatively long life of their own in the computer's memory.
250+
251+
175252Vendorizing Dependencies
176253------------------------
177254
0 commit comments