Python
Python is a dynamically-typed high-level programming language whose design philosophy centres around readability and terseness. It’s used frequently for data science and machine learning, backend development, scripts and CLIs.
Code is sometimes described as pythonic, which just means it exploits Python’s language features and design to produce something very readable and elegant.
# Dunder Methods
TODO.
# Context Manager
with
TODO: see https://www.pythoncheatsheet.org/cheatsheet/context-manager.
# Virtual Environments
Always use a virtual environment. source
To prevent bloating the base Python installation with project dependencies and have reproducible/portable setups, use virtual environments.
|
|
# Import and Export
- Modules — any Python file.
- Packages — directories containing
__init__.py
. A package contains one or multiple modules or further nested packages.
|
|
- Searches for the
foo
module in the paths insys.path
. - Creates a
module object and assigns it to the
foo
variable.
|
|
- Searches for the
foo
package in the paths insys.path
, and then finds thebar
module in that package. - Creates a module object and assigns it to the
foo
variable.
|
|
- Searches for
foo
in the paths insys.path
. - Creates a module object and assigns it to the
foo
variable. - Declares and initialises variables
bar = foo.bar
andbaz = foo.baz
.- Note: if the exported binding
foo.bar
doesn’t exist, then Python attempts to interpretbar
as a submodule, so it triesimport foo.bar
as a fallback.
- Note: if the exported binding
- Deletes the
foo
variable.
# Relative Imports
Relative imports are generally discouraged since they’re less readable, less understood, and easy to break.
All relative imports are done with from _ import _
. The import _
statement is always absolute.
|
|
.
is the current package, which is what__package__
is set to...
is the parent package.
Having trouble? Some crucial details to note:
- Python files are loaded as either a module or a top-level script. When you do
python app.py
, you are loadingapp.py
as a top-level script. - Top-level scripts have
__name__
set to__main__
. There is only ever 1 top-level script. - Modules have
__name__
set to a dot-separated string of their package path. - The
__name__
is used to determine where the current file is in a package, not the filesystem structure like in other languages. If__name__
is__main__
, then you’ll get theno known parent package
error.
# __init__.py
The presence of this file (even if empty) indicates that the containing folder is a package, not a regular directory. This rule was dropped for subpackages, however, to improve developer experience.
Whatever you import inside __init__.py
becomes accessible directly under the package name for consumers. Eg. in the example below, consumers can just do from foo import baz
. This works a bit similarly to the index.js
file exporting variables in JavaScript.
|
|
# Classes
Take notes from here: https://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python
To make a member private, prefix its name with an underscore _
. This doesn’t actually do anything, it’s just an agreed upon standard for documenting something should be private. There’s nothing stopping users of the class from invoking private methods.
- Name mangling ensures that subclasses don’t unknowingly override a private function with the same name.
TODO: @classmethod
decorator. See
https://stackoverflow.com/questions/54264073/what-is-the-use-and-when-to-use-classmethod-in-python
# Getter and Setter
|
|
# Type System
Python is dynamically-typed and will always stay that way, however you can still opt to add static types wherever you find it useful, like how you might mix JavaScript and TypeScript code. Static typing is massively helpful in large projects as opposed to one-off scripts.
# Type Hints
Type hints do nothing at runtime. You have to use a static type checker such as Mypy, or PyLance with VSCode, for example.
Typing variables and functions in Python is very similar to how it’s done in TypeScript.
|
|
# typing Module
Python 3.5 supplies the
typing
built-in module brings in a lot of advanced static typing utilities such as those seen in TypeScript.
# Standard Built-In Modules
See standard built-in modules.
# File Manipulation
Python has built-in top-level functions for file manipulation:
|
|
# OS
os
provides a bunch of useful functions for working with the filesystem.
|
|
# Regex
See regex.
Use raw strings
r"..."
when specifying regex patterns to avoid being confused about what characters are being escaped.
|
|
Capture Groups:
|
|
# Dates
Use
datetime
to work with dates. Use
time
for lower-level operations involving time.
Convert between strings and dates:
|
|
Date arithmetic and comparison:
Use timedelta
to add/subtract time from a date. You can directly use comparison operators on datetime
objects.
|
|