@@ -330,42 +330,42 @@ Easy structuring of a project means it is also easy
330
330
to do it poorly. Some signs of a poorly structured project
331
331
include:
332
332
333
- - Multiple and messy circular dependencies: if your classes
333
+ - Multiple and messy circular dependencies: If the classes
334
334
Table and Chair in :file: `furn.py ` need to import Carpenter from
335
335
:file: `workers.py ` to answer a question such as ``table.isdoneby() ``,
336
336
and if conversely the class Carpenter needs to import Table and Chair
337
337
to answer the question ``carpenter.whatdo() ``, then you
338
338
have a circular dependency. In this case you will have to resort to
339
- fragile hacks such as using import statements inside
339
+ fragile hacks such as using import statements inside your
340
340
methods or functions.
341
341
342
- - Hidden coupling: each and every change in Table's implementation
342
+ - Hidden coupling: Each and every change in Table's implementation
343
343
breaks 20 tests in unrelated test cases because it breaks Carpenter's code,
344
- which requires very careful surgery to adapt the change. This means
344
+ which requires very careful surgery to adapt to the change. This means
345
345
you have too many assumptions about Table in Carpenter's code or the
346
346
reverse.
347
347
348
- - Heavy usage of global state or context: instead of explicitly
348
+ - Heavy usage of global state or context: Instead of explicitly
349
349
passing ``(height, width, type, wood) `` to each other, Table
350
350
and Carpenter rely on global variables that can be modified
351
351
and are modified on the fly by different agents. You need to
352
- scrutinize all access to these global variables to understand why
352
+ scrutinize all access to these global variables in order to understand why
353
353
a rectangular table became a square, and discover that remote
354
354
template code is also modifying this context, messing with
355
- table dimensions.
355
+ the table dimensions.
356
356
357
357
- Spaghetti code: multiple pages of nested if clauses and for loops
358
358
with a lot of copy-pasted procedural code and no
359
359
proper segmentation are known as spaghetti code. Python's
360
- meaningful indentation (one of its most controversial features) make
361
- it very hard to maintain this kind of code. So the good news is that
360
+ meaningful indentation (one of its most controversial features) makes
361
+ it very hard to maintain this kind of code. The good news is that
362
362
you might not see too much of it.
363
363
364
364
- Ravioli code is more likely in Python: it consists of hundreds of
365
365
similar little pieces of logic, often classes or objects, without
366
- proper structure. If you never can remember if you have to use
366
+ proper structure. If you never can remember, if you have to use
367
367
FurnitureTable, AssetTable or Table, or even TableNew for your
368
- task at hand, you might be swimming in ravioli code.
368
+ task at hand, then you might be swimming in ravioli code.
369
369
370
370
371
371
*******
@@ -383,24 +383,24 @@ in one file, and all low-level operations in another file. In this case,
383
383
the interface file needs to import the low-level file. This is done with the
384
384
``import `` and ``from ... import `` statements.
385
385
386
- As soon as you use `import ` statements you use modules. These can be either
386
+ As soon as you use `import ` statements, you use modules. These can be either
387
387
built-in modules such as `os ` and `sys `, third-party modules you have installed
388
388
in your environment, or your project's internal modules.
389
389
390
390
To keep in line with the style guide, keep module names short, lowercase, and
391
391
be sure to avoid using special symbols like the dot (.) or question mark (?).
392
- So a file name like :file: `my.spam.py ` is one you should avoid! Naming this way
392
+ A file name like :file: `my.spam.py ` is the one you should avoid! Naming this way
393
393
will interfere with the way Python looks for modules.
394
394
395
395
In the case of `my.spam.py ` Python expects to find a :file: `spam.py ` file in a
396
396
folder named :file: `my ` which is not the case. There is an
397
397
`example <http://docs.python.org/tutorial/modules.html#packages >`_ of how the
398
398
dot notation should be used in the Python docs.
399
399
400
- If you'd like you could name your module :file: `my_spam.py `, but even our
401
- friend the underscore should not be seen often in module names. However, using other
400
+ If you like, you could name your module :file: `my_spam.py `, but even our trusty
401
+ friend the underscore, should not be seen that often in module names. However, using other
402
402
characters (spaces or hyphens) in module names will prevent importing
403
- (- is the subtract operator), so try to keep module names short so there is
403
+ (- is the subtract operator). Try to keep module names short so there is
404
404
no need to separate words. And, most of all, don't namespace with underscores; use submodules instead.
405
405
406
406
.. code-block :: python
@@ -411,15 +411,15 @@ no need to separate words. And, most of all, don't namespace with underscores; u
411
411
import library.foo_plugin
412
412
413
413
Aside from some naming restrictions, nothing special is required for a Python
414
- file to be a module, but you need to understand the import mechanism in order
414
+ file to be a module. But you need to understand the import mechanism in order
415
415
to use this concept properly and avoid some issues.
416
416
417
417
Concretely, the ``import modu `` statement will look for the proper file, which
418
- is :file: `modu.py ` in the same directory as the caller if it exists. If it is
418
+ is :file: `modu.py ` in the same directory as the caller, if it exists. If it is
419
419
not found, the Python interpreter will search for :file: `modu.py ` in the "path"
420
- recursively and raise an ImportError exception if it is not found.
420
+ recursively and raise an ImportError exception when it is not found.
421
421
422
- Once :file: `modu.py ` is found, the Python interpreter will execute the module in
422
+ When :file: `modu.py ` is found, the Python interpreter will execute the module in
423
423
an isolated scope. Any top-level statement in :file: `modu.py ` will be executed,
424
424
including other imports if any. Function and class definitions are stored in
425
425
the module's dictionary.
@@ -436,7 +436,7 @@ unwanted effects, e.g. override an existing function with the same name.
436
436
437
437
It is possible to simulate the more standard behavior by using a special syntax
438
438
of the import statement: ``from modu import * ``. This is generally considered
439
- bad practice. **Using ** ``import * `` **makes code harder to read and makes
439
+ bad practice. **Using ** ``import * `` **makes the code harder to read and makes
440
440
dependencies less compartmentalized **.
441
441
442
442
Using ``from modu import func `` is a way to pinpoint the function you want to
@@ -492,20 +492,20 @@ modules, but with a special behavior for the :file:`__init__.py` file, which is
492
492
used to gather all package-wide definitions.
493
493
494
494
A file :file: `modu.py ` in the directory :file: `pack/ ` is imported with the
495
- statement ``import pack.modu ``. This statement will look for an
495
+ statement ``import pack.modu ``. This statement will look for
496
496
:file: `__init__.py ` file in :file: `pack ` and execute all of its top-level
497
497
statements. Then it will look for a file named :file: `pack/modu.py ` and
498
498
execute all of its top-level statements. After these operations, any variable,
499
499
function, or class defined in :file: `modu.py ` is available in the pack.modu
500
500
namespace.
501
501
502
- A commonly seen issue is to add too much code to :file: `__init__.py `
502
+ A commonly seen issue is adding too much code to :file: `__init__.py `
503
503
files. When the project complexity grows, there may be sub-packages and
504
504
sub-sub-packages in a deep directory structure. In this case, importing a
505
505
single item from a sub-sub-package will require executing all
506
506
:file: `__init__.py ` files met while traversing the tree.
507
507
508
- Leaving an :file: `__init__.py ` file empty is considered normal and even a good
508
+ Leaving an :file: `__init__.py ` file empty is considered normal and even good
509
509
practice, if the package's modules and sub-packages do not need to share any
510
510
code.
511
511
@@ -519,45 +519,44 @@ Object-oriented programming
519
519
***************************
520
520
521
521
Python is sometimes described as an object-oriented programming language. This
522
- can be somewhat misleading and needs to be clarified .
522
+ can be somewhat misleading and requires further clarifications .
523
523
524
524
In Python, everything is an object, and can be handled as such. This is what is
525
525
meant when we say, for example, that functions are first-class objects.
526
526
Functions, classes, strings, and even types are objects in Python: like any
527
527
object, they have a type, they can be passed as function arguments, and they
528
- may have methods and properties. In this understanding, Python is an
529
- object-oriented language.
528
+ may have methods and properties. In this understanding, Python can be considered
529
+ as an object-oriented language.
530
530
531
531
However, unlike Java, Python does not impose object-oriented programming as the
532
532
main programming paradigm. It is perfectly viable for a Python project to not
533
533
be object-oriented, i.e. to use no or very few class definitions, class
534
534
inheritance, or any other mechanisms that are specific to object-oriented
535
- programming.
535
+ programming languages .
536
536
537
537
Moreover, as seen in the modules _ section, the way Python handles modules and
538
538
namespaces gives the developer a natural way to ensure the
539
539
encapsulation and separation of abstraction layers, both being the most common
540
540
reasons to use object-orientation. Therefore, Python programmers have more
541
- latitude to not use object-orientation, when it is not required by the business
541
+ latitude as to not use object-orientation, when it is not required by the business
542
542
model.
543
543
544
544
There are some reasons to avoid unnecessary object-orientation. Defining
545
- custom classes is useful when we want to glue together some state and some
546
- functionality. The problem, as pointed out by the discussions about functional
545
+ custom classes is useful when we want to glue some state and some
546
+ functionality together . The problem, as pointed out by the discussions about functional
547
547
programming, comes from the "state" part of the equation.
548
548
549
549
In some architectures, typically web applications, multiple instances of Python
550
- processes are spawned to respond to external requests that can happen at the
551
- same time. In this case, holding some state in instantiated objects, which
550
+ processes are spawned as a response to external requests that happen simultaneously.
551
+ In this case, holding some state in instantiated objects, which
552
552
means keeping some static information about the world, is prone to concurrency
553
553
problems or race conditions. Sometimes, between the initialization of the state
554
554
of an object (usually done with the ``__init__() `` method) and the actual use
555
555
of the object state through one of its methods, the world may have changed, and
556
556
the retained state may be outdated. For example, a request may load an item in
557
557
memory and mark it as read by a user. If another request requires the deletion
558
- of this item at the same time, it may happen that the deletion actually occurs
559
- after the first process loaded the item, and then we have to mark as read a
560
- deleted object.
558
+ of this item at the same time, the deletion may actually occur after the first
559
+ process loaded the item, and then we have to mark a deleted object as read.
561
560
562
561
This and other issues led to the idea that using stateless functions is a
563
562
better programming paradigm.
@@ -571,7 +570,7 @@ or deletes data in a global variable or in the persistence layer, it is said to
571
570
have a side-effect.
572
571
573
572
Carefully isolating functions with context and side-effects from functions with
574
- logic (called pure functions) allow the following benefits:
573
+ logic (called pure functions) allows the following benefits:
575
574
576
575
- Pure functions are deterministic: given a fixed input,
577
576
the output will always be the same.
@@ -713,7 +712,7 @@ type. In fact, in Python, variables are very different from what they are in
713
712
many other languages, specifically statically-typed languages. Variables are not
714
713
a segment of the computer's memory where some value is written, they are 'tags'
715
714
or 'names' pointing to objects. It is therefore possible for the variable 'a' to
716
- be set to the value 1, then to the value 'a string', then to a function.
715
+ be set to the value 1, then the value 'a string', to a function.
717
716
718
717
The dynamic typing of Python is often considered to be a weakness, and indeed
719
718
it can lead to complexities and hard-to-debug code. Something named 'a' can be
@@ -743,7 +742,7 @@ Some guidelines help to avoid this issue:
743
742
def func ():
744
743
pass # Do something
745
744
746
- Using short functions or methods helps reduce the risk
745
+ Using short functions or methods helps to reduce the risk
747
746
of using the same name for two unrelated things.
748
747
749
748
It is better to use different names even for things that are related,
@@ -845,7 +844,7 @@ most idiomatic way to do this.
845
844
846
845
One final thing to mention about strings is that using ``join() `` is not always
847
846
best. In the instances where you are creating a new string from a pre-determined
848
- number of strings, using the addition operator is actually faster, but in cases
847
+ number of strings, using the addition operator is actually faster. But in cases
849
848
like above or in cases where you are adding to an existing string, using
850
849
``join() `` should be your preferred method.
851
850
0 commit comments