5.10. SummaryΒΆ

In this chapter we have covered items specific to Python that help structure Python programs.

  1. Import statements are used t import code from modules that are part of a standard Python distribution or written by others in Python or written by you. It really doesn’t matter. In principle, a module is just a file containing legal python code, usually in the form of function, class, and variable definitions.

  2. Python namespaces. By default every Python module has its own private namespace, accessible by using <modulename>.<name>. Thus, different modules can use the same names without causing name clashes. A name from a module can be imported into the top level namespace of your program by using the construction:

    from <modulename> import <name>
    

    Import statements are generally all written at the top of a file of Python code.

  3. Block structure. Readability of a Python program is generally enhanced by the fact that code is broken up into blocks by certain Python constructions. The key block-creating constructions we’ve covered are: function definitions, loops, if-then constructions, class definitions, method definitions.

  4. Parameters of a function may be filled either by following the order of the parameters in the function definition or by using keywords. Thus for a function defined as:

    def foo(param1,param2):

    code

    The following two ways of calling foo are equivalent

    foo(x,y) foo(param2=y,param1=x)