Search notes:

Python

Install Python

Windows

On Windows, Python can be installed with Chocolatey:
choco install -y python
A pre-release version can be installed with the --pre option:
choco install -y python --pre 
An already installed Python version can be upgraded to the most recent one with
choco upgrade -y python
# or
choco upgrade -y python --pre

Packages and modules

Analogy: However, there is no need for packages and modules to actually exist as files or directories.
All packages are also modules, but only modules with a __path__ attribute are also a package.
There are two types of packages:
Code within one module can access code in another module by importing it.
See also sys.modules.

Python Package Index (PyPI)

The Python Package Index (PyPI) is, analogous to Perl's CPAN or R's CRAN, the third party repository for Python.
Packages hosted on PyPI can be downloaded and installed with package manages such as pip.
There are two forms in which packages are provided:
  • sdists - which re source distributions
  • wheel files - where the sources are precompiled
PyPI must not be confused with PyPy

Packaging/deployment of modules

PEP-517, PEP-518

Objects

Objects

__builtins__

__builtins__ is a module that is loaded when a Python interpreter is started up:
>>> type(__builtins__)
<class 'module'>
This module contains Python's «built-in» stuff, such as built-in functions or exceptions.
The module's content can be browsed with dir() (which itself is located in the __builtins__ module):
>>> __builtins__.dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError',
'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError',
'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError',
'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning',
'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning',
'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning',
'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any',
'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',
'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', ' len', 'license', 'list', 'locals', 'map', 'max',
'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple',
'type', 'vars', 'zip']

Scopes and namespaces

A namespace maps names to objects and are typically implemented using Python-dictionaries.
Some namespaces include
A Python statement executes in a (nested) scope which determines how identifiers are resolved so that it is not always necessary to write ns.ident to refer to ident in namespace ns.
The order of searching is:
Innermost (local) scope This scope contains the local names
Scopes of call-chain If an identifier is not found in the innermost scope, the interpreter tries to resolve them by going up the call-chain (call-stack, if you will)
Current module's global names If still not found, the current module's global names are tried. This is also the scope where globally declared names live.
__builtins__ As a last resort, the __builtins__ (and others?) namespace is searched.
With the exception of using the global or nonlocal statement, all identifiers are created in the namespace that is associated with the local scope, notably by assigning a value to non existing name or when using the import statement.
Another exception to that «rule» is from MODULE import *: it places the identifiers of MODULE in the global namespace.
The name of the scope in which top-level code executes is '__main__'.

Attributes

An attribute is anything after a dot.
Attributes may be read-only or writable.
The value of writable attributes can be changed with the assignment operator, and writable attributes can be removed with del() statement. (TODO: can they also be removed with the delattr() built-in function?)

Python scripts

Arguments

Arguments that were passed to a Python script can be accessed with sys.argv:
import sys

if len(sys.argv) < 4:
   print('Script expected at least three arguments')
   sys.exit(-1)

#
# Script name is in sys.argv[0].
# Save value and remove from list:
#
scriptName = sys.argv[0]
del sys.argv[0]

print('Arguments that were passed to ' + scriptName + ':')
for arg in sys.argv:
    print('  - ' + arg)

UTF-8 Source code

By default, the Python interpreter assumes source code to be encoded in UTF-8 in Python 3 (ASCII in Python 2).
The encoding of a file can be epxlicitely set to a specific encoding (ENCODING-NAME, for example cp1252) with the following first line (after the optional shebang) in a source file
# -*- coding: ENCODING-NAME -*-

.pyd files

.pyd files are Python DLLs. Such files can be imported with import xyz (without stating the .pyd suffix). This DLL must be located in a directory that is pointed at with the PYTHONPATH environment variable(?) in order to be found.
Such a .pyd file must export a PyInit_xyz() function (xyz corresponds to the filename).

Misc

Python assumes an unsigned char to be 8-bit (as per comment in Python.h)
During initialization, Lib/site.py is automatically imported. This module appends site-specific paths to the module search path.
With the May 2019 update of Windows 10, Python comes with the Windows installation.

Gotchas and landmines

class variables

#
#     http://stackoverflow.com/a/531327/180275
#

class AAA(object):
      x = 1

class BBB(AAA):
      pass

class CCC(BBB):
      pass


print AAA.x, BBB.x, CCC.x   # 1 1 1

BBB.x = 2

print AAA.x, BBB.x, CCC.x   # 1 2 2

AAA.x = 3

print AAA.x, BBB.x, CCC.x   # 3 2 2

a = AAA()

print a.x, AAA.x            # 3 3

a.x = 4

print a.x, AAA.x            # 4 3

Exception handling

Unintended?
#
#   http://stackoverflow.com/a/4403680/180275
#
#   Compare with exception_handling_intended.py

try:
    int("z")
except IndexError, ValueError:
    print "exception";          # not caught!
Intended?
#
#   http://stackoverflow.com/a/4403680/180275
#
#   Compare with exception_handling_unintended.py

try:
    int("y")
except (IndexError, ValueError):
    print "exception"

Dynamic binding

#
#    http://stackoverflow.com/a/530577/180275
#

some_list       = ['foo', 'bar', 'baz'  ]
some_other_list = ['one', 'two', 'three']

print "some_list"
for item in some_list:
    print item

#   foo
#   bar
#   baz

print "some_other_list"
for iten in some_other_list:  # note the typo: "iten" instead of "item"
    print item

#   baz
#   baz
#   baz

Loops and lambdas

#
#   http://stackoverflow.com/a/531376/180275
#
funcs = []

for x in range(5):
    funcs.append(lambda: x)

print [f() for f in funcs]     #  [4, 4, 4, 4, 4]

# ---

funcs = []
for x in range(5):
  funcs.append(lambda x=x: x)
  
print [f() for f in funcs]    #  [0, 1, 2, 3, 4]

Default arguments

#!/usr/bin/python3
#
#      http://stackoverflow.com/a/530768/180275
#
import time

def report_time_wrong(when=time.time()):
    print (when)

report_time_wrong()   # 1544219102.503972
time.sleep(5)
                      # Note, same time again
report_time_wrong()   # 1544219102.503972


def report_time_better(when=None):
    if when is None:
       when = time.time()

    print (when)

report_time_better()  # 1544219107.5091524
time.sleep(5)
report_time_better()  # 1544219112.5136003

Wheel

A wheel is a package in zip file format and the file name exentsion .whl.
The advantage of the wheel format is that a package does not need to be recompiled when it is installed on a particular machine. In fact, the archive can be extracted into the Lib/site-packages directory (for example using unzip, although a specialized installer (such as pip?) is recommended).
Wheels are intended to replace the egg format (which was the format that came with setuptools)
The wheel format is defined in PEP 427.
See also

PyPy

PyPy is a Just-in-Time engine to run Python scripts and a replacement for CPython to run Python scripts more performantly.
PyPy especially shines for scripts where time is not spent in C-functions and scripts that run longer than a few seconds.
The PyPy interpreter is written in RPython (which stands for restricted Python).
PyPy must not be confused with PyPI.

Special interest groups

There are a few special interest groups which try to collaboratively develop, improve or maintain Python resources.
These groups have a -sig suffix, for example

Line continuation

The following nicely formatted code, unfortunately, throws a SyntaxError expection (invalid syntax):
a, b, c, d = 3.1, 2.4, 1.9, 4.1
x          = 4
y = a * x**3 +
    b * x**2 +
    c * x**1 +
    d * x**0
In order to make it run, the lines need to be ended with an ugly backslash:
y = a * x**3 + \
    b * x**2 + \
    c * x**1 + \
    d * x**0

Web application frameworks

Web application frameworks include
  • Zope
  • Quixote
  • Webware
  • SkunWEb
  • PSO
  • TwistedWeb
See also

Influences from other languages

Some Python features are influenced by other languages such as:
  • some basic syntactic features from C
  • exceptions from Modula-3
  • classes from C++
  • slicing from Icon
  • regular expressions from Perl
  • decorators from Java annotations
  • etc.

See also

A PEP is a Pyhon Enhancement Proposal.
Python code snippets
statements and operators
standard library
qualified name
exception handling
The string and dict types.
~/.pythonrc.py
On Windows, py.exe can be used to execute a Python script under a specific version.
Other programming languages etc.
The reference implementation for Python is CPython.
On Windows: %LOCALAPPDATA%\Programs\Python

Index