paulgorman.org/technical

Python3 Refresher (2023)

https://docs.python.org/3/tutorial/index.html

Virtual Environments

When starting a Python project, create a virtul environment to avoid various problems like conflicting global library versions. Activate the virtual environment.

https://docs.python.org/3/tutorial/venv.html

🐚 $ mkdir my-python-project
🐚 $ python3 -m venv ./my-python-project/venv
🐚 $ cd my-python-project/
🐚 $ ls venv/
bin             include         lib             pyvenv.cfg
🐚 $ source venv/bin/activate
(my-python-project) 🐚 $ 
(my-python-project) 🐚 $ deactivate
🐚 $ 

Remember to add venv/ to .gitignore.

(On Windows, activate the virtual environment with venv\Scripts\activate.)

Package Management

Manage packages with pip.

(Browse packages in the default repository at https://pypi.org/.)

🐚 $ source bin/activate
(my-python-project) 🐚 $ pip install -U Flask
Collecting Flask
  Downloading flask-3.0.0-py3-none-any.whl.metadata (3.6 kB)
[…]
Installing collected packages: MarkupSafe, itsdangerous, click, blinker, Werkzeug, Jinja2, Flask
Successfully installed Flask-3.0.0 Jinja2-3.1.2 MarkupSafe-2.1.3 Werkzeug-3.0.1 blinker-1.7.0 click-8.1.7 itsdangerous-2.1.2
(my-python-project) 🐚 $ 

A requirements.txt file is helpful both to document the required packages our project uses and to install the requirements (i.e., if we or someone else starts working on the project in a new environment):

PySide2>=5.11.1
appdirs

This requirements.txt file can be used by Pip to install the required packages:

🐚 $ pip install -r requirements.txt

Variable Scope

Variables are classified with a scope that Python sets when a variable is first assigned a value.

Python variable scopes are, from inner to outer: local to a function, within the enclosing function, global (module) scope, and finally built-in scope. These scope rules are sometimes described with the mnemonic “LEGB” (Local, Enclosing, Global, Built-In).

Python searches scopes from the inside/smallest/narrowest to the outside/largest/widest. Variables in narrower scopes will (by default) mask variables in wider scopes.

The global and nonlocal keywords can override the default scoping.

There’s no truly global scope β€” “global” variables are contained in the namespace of their module.

These scope rules apply only to variables (my_variable) not object attributes (myObject.myAttribute).

>>> import builtins
>>> dir(builtins)

There are also three other scopes: temporary loop variables in some comprehensions, exception reference variables in some try handlers, and local scopes in class statements.

Modules

Every Python file is its own module.

Comments, Docstrings and PyDoc

# starts a comment, anywhere in a line.

PyDoc is the Python documentation tool. It reads docstrings in code and outputs documentation.

"""
This is the docstring introducing this module.
Details, details….
"""

def double(n):
	"""This docstring summarizes the function's purpose and does NOT just reiterate the function signature. """
	return 2 * n

print(double(2))

Python adds docstrings it finds to the __doc__ attribute of corresponding objects. If we save the above code the file docstringsTest.py, we can:

🐚 $ python
>>> import docstringsTest
>>> print(docstringsTest.__doc__)
>>> print(docstringsTest.double.__doc__)

(See also PEP 257 for docstring conventions.)

Style

There is a Style Guide for Python Code.

Static Analysis

Pylint is a popular linter that can be installed by Pip.

🐚 $ pylint myfile.py

References