Skip to content




Forced name params

  • pass a * to separate positional args and keyword args
def foo(pos, *, forcenamed, **kwargs):

my_func(1, 2) # throws an error

Mark a function as deprecated


def yell():

bark = yell

del yell  # only deletes one pointer to the function

bark()  # still in the heap because there's still one pointer left


or start a file with PDB

python -m pdb


-m flag: Module

python -m module_name args

  • run a module
  • python -m http.server
    • start http server on port 8000 in current directory
  • python -m pdb path/to/
    • debug a Python script
  • python -m pip install ...
    • guarantee that you're running the version of pip that's connected to the current python

-i flag: Interactive

Debug the state of a broken Python code when it crashed!

Although you should use breakpoint() and pdb

$ python3 -i 4 7
Traceback (most recent call last):
  File "/home/trey/", line 5, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

# can debug to see the state of the program when it crashed!
>>> __name__
>>> sys.argv
['', '4', '7']
>>> numbers
['4', '7']

File system


from pathlib import Path

file_directory = Path(__file__).parent.resolve()
another_path = file_directory / "sql_scripts"

Get an environment variable value

os.environ.get('ENV_VAR_NAME', 'FALLBACK_VALUE')

os.getenv() is an alias to os.environ.get()

Virtual environment

Uninstall all packages in a virtual env


== vs is

  • is: same ID
    • place in the stack/heap
    • id(x) => 140079600030400
  • ==: uses __eq__
    • by default, == delegates to is

isinstance vs type()

  • type: returns the name of the type
  • isinstance([1,2,3], list)
    • goes up the inheritance tree
    • can catch more than comparing two types

walrus operator

match =
if match:
if match :=
while chunk :=
  • new in Python 3.8
  • assignment expression
    • merge two lines into one
  • assignment statements needs to be on their own line
  • When to use?
    • only if it makes your code more readable

Last update: 2022-11-04