Summary

  • Python modules:

    • Python code is placed in *.py files called modules.

    • Modules can be executed directly by passing them as the first argument to the Python interpreter.

    • Modules can also be imported into the REPL, at which point all top-level statements in the module are executed in order.

  • Python functions:

    • Named functions are defined using the def keyword followed by the function name and the argument list in parentheses.

    • We can return objects from functions using the return statement.

    • Return statements without a parameter return None, as does the implicit return at the end of every function body.

  • Module execution:

    • We can detect whether a module has been imported or executed by examining
      the value of the special __name__ variable. If it is equal to the string
      "__main__" our module has been executed directly as a program. By executing a function if this condition is met using the top-level if __name__ == '__main__' idiom at the end of our module, we can make our module both usefully importable and executable, an important testing technique even for short scripts.

    • Module code is only executed once, on first import.

    • The def keyword is a statement which binds executable code to a function name.

    • Command line arguments can be accessed as a list of strings accessible through the argv attribute of the sys module. The zero-th command line argument is the script filename, so the item at index one is the first true argument.

    • Python's dynamic typing means our functions can be very generic with respect to the type of their arguments.

  • Docstrings:

    • A literal string as the first line of a function's definition forms the function's docstring. They are typically triple-quoted multiline strings containing usage information.

    • Function documentation provided in docstrings can be retrieved using help() in the REPL.

    • Module docstrings should be placed near the beginning of the module prior to any Python statements such as import statements.

  • Comments:

    • Comments in Python commence with a hash character and continue to the end of the line.

    • The first line of the module can contain a special comment called a shebang, allowing the program loader to launch the correct Python interpreter on all major platforms.

  1. Technically modules don't have to be simple source code files, but for the purposes of this book that is a sufficient definition.

  2. Technically some of these compiled languages do provide mechanisms for defining functions dynamically at runtime. However, these methods are by far the exception rather than the rule in almost all situations.

  3. Python code is actually compiled to byte-code, so in that sense Python has a compiler. But the compiler is doing substantially different kinds of work than what you might be used to from popular compiled, statically-typed languages.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset