diff --git a/docs/correctness/access_to_a_protected_member.rst b/docs/correctness/access_to_a_protected_member.rst index 4772f63..1b67c94 100644 --- a/docs/correctness/access_to_a_protected_member.rst +++ b/docs/correctness/access_to_a_protected_member.rst @@ -9,13 +9,13 @@ A module has attempted to directly access a protected member (a member prefixed Description ----------- -Treat this error as a warning. Although it violates the principles of object-oriented programming to directly access protected members, Python will have no problem executing the code. +Treat this error as a warning. Although it violates the principles of object-oriented programming to directly access protected or private members, Python will have no problem executing the code. Examples ---------- -Module directly accesses protected member of class -.................................................. +Problem: Module directly accesses protected member of class +........................................................... The module below directly accesses the ``_width`` member of a Rectangle instance. ``_width`` is protected because it is prefixed with ``_``. This indicates that no code outside of the class is supposed to directly access the member. @@ -31,11 +31,8 @@ The module below directly accesses the ``_width`` member of a Rectangle instance r = Rectangle(5, 6) print "Width: {:d}".format(r._width) # direct access of protected member -Solutions ---------- - -Use ``property`` to access the member -..................................... +Solution: Use ``property`` to access the member +''''''''''''''''''''''''''''''''''''''''''''''' For new-style classes (classes that derive from ``object``, e.g. ``class Rectangle(object):``) the Pythonic way to access members is to use the built-in Python function ``property()``. @@ -63,8 +60,8 @@ For new-style classes (classes that derive from ``object``, e.g. ``class Rectang r = Rectangle(5, 6) print "Width: {:d}".format(r.width) # automatically accesses getter, behind the scenes -Add protected member to ``__all__`` -................................... +Solution: Add protected member to ``__all__`` +''''''''''''''''''''''''''''''''''''''''''''' If the author of the class decides that it is accessible to directly access the protected member, he can add the protected member to the module's ``__all__`` class to indicate this. The code will execute either way, but this at least explicitly documents that it is acceptable to directly access the member. diff --git a/docs/correctness/class_has_no_member.rst b/docs/correctness/class_has_no_member.rst index 1c95dde..6473c56 100644 --- a/docs/correctness/class_has_no_member.rst +++ b/docs/correctness/class_has_no_member.rst @@ -14,8 +14,8 @@ When a module attempts to use a member of a class which is not defined in that c Examples ---------- -Description of error -.................... +Problem: Module calls unimplemented function +............................................ The module below attempts to call a function from the ``Rectangle`` class called ``area``. This raises the ``class has no member`` error because the ``area`` function in undefined in the ``Rectangle`` class. @@ -32,12 +32,8 @@ The module below attempts to call a function from the ``Rectangle`` class called print r.area() # no such member in Rectangle class - -Solutions ---------- - -Implement the undefined member -.............................. +Solution: Implement the undefined member +'''''''''''''''''''''''''''''''''''''''' The updated module below implements the ``area`` member. Now that the function is defined, the ``class has no member`` error is suppressed. @@ -54,8 +50,8 @@ The updated module below implements the ``area`` member. Now that the function i print r.area() # ok now -Remove the reference to the undefined member -............................................ +Solution: Remove the reference to the undefined member +'''''''''''''''''''''''''''''''''''''''''''''''''''''' The updated module below suppresses the ``class has no member`` error by using known defined class members to work around the problem. Rather than trying to use the undefined member ``area``, the module multiples the ``width`` and ``height`` attributes of the instance to get the desired value. diff --git a/docs/correctness/function_keyword_outside_of_function.rst b/docs/correctness/function_keyword_outside_of_function.rst index 020e788..6b69c7a 100644 --- a/docs/correctness/function_keyword_outside_of_function.rst +++ b/docs/correctness/function_keyword_outside_of_function.rst @@ -14,8 +14,8 @@ This article is generally applicable to the two errors ``Yield outside function` Examples -------- -``return`` used outside of function -................................... +Problem: ``return`` used outside of function +............................................ The module below is frequently imported into other modules. The author is trying to generate a random value and return this value to the other modules that import it. The author mistakenly believes that using a ``return`` statement at the global level of this module will accomplish this functionality. In reality Python does not allow this. The ``return`` keyword can only be used inside of functions. Python will raise a ``SyntaxError`` and the module will not execute successfully. @@ -32,23 +32,8 @@ The module below is frequently imported into other modules. The author is trying return r # SyntaxError: return outside function -``yield`` used outside of function -................................... - -The author of the module below is new to Python and does not understand how ``yield`` is used. - -.. warning:: The code below is an example of an error. Using this code will create bugs in your programs! - -.. code:: python - - for n in range(10): - yield n - -Solutions ---------- - -Remove the ``return`` statement -............................... +Solution: Remove the ``return`` statement +''''''''''''''''''''''''''''''''''''''''' The modified module below suppresses the ``return outside function`` by deleting the return statement. @@ -63,8 +48,8 @@ The modified module below suppresses the ``return outside function`` by deleting # deleted return statement here -Wrap the ``return`` statement in a function -........................................... +Solution: Wrap the ``return`` statement in a function +..................................................... The modified module below suppresses the ``return outside function`` by wrapping the ``return`` statement inside of a function. Whenever another module imports this module and wants to get a random number, the other module can just call ``import random_nums`` and then call ``random_nums.get_random_number()``. @@ -78,8 +63,22 @@ The modified module below suppresses the ``return outside function`` by wrapping print "Your random number is", r return r # wrapped up the return statement inside of a function -Remove the ``yield`` statement -.............................. + +Problem: ``yield`` used outside of function +........................................... + +The author of the module below is new to Python and does not understand how ``yield`` is used. + +.. warning:: The code below is an example of an error. Using this code will create bugs in your programs! + +.. code:: python + + for n in range(10): + yield n + + +Solution: Remove the ``yield`` statement +''''''''''''''''''''''''''''''''''''''''' The modified module below suppresses the ``yield outside function`` by replacing the ``yield`` statement with a ``print`` statement. This is just an example, the solution will depend on what you are trying to accomplish.