Using virtual environments in Python — Python Morsels

Trey Hunner 6 minute read • Python 3.7—3.10 Watch as video 03:58 Let’s talk about using virtual environments in Python. Python code with third-party requirements Here we have a Python program called exponential.py that uses a module called rich: from argparse import ArgumentParser from rich.console import Console from rich.table import Table parser = ArgumentParser() parser.add_argument(“start”, … Read more

Appreciating Python’s match-case by parsing Python code — Python Morsels

Trey Hunner 9 minute read • Python 3.10 I stayed up past my bedtime recently and made a script and later a web app to convert a dataclass to a non-dataclass. The web app is powered by a WebAssembly build of Python (which also powers my Python pastebin tool). While making this script I found … Read more

Python’s setattr function — Python Morsels

Trey Hunner 5 minute read • Python 3.7—3.10 The setattr function is one of the lesser used built-in functions in Python. Let’s talk about Python’s built-in setattr function. Need to dynamically set an attribute? We’d like to make a class that works like this: >>> row = Row(id=4, name=”duck”, action=”quack”, color=”purple”) >>> row.id 4 >>> … Read more

Python’s “functions” are sometimes classes — Python Morsels

Trey Hunner 10 minute read • Works on Python 3.7—3.10 If you search course curriculum I’ve written, you’ll often find phrases like “zip function”, “enumerate function”, and “list function”. Those terms are all technically misnomers. When I use terms like “the bool function” and “the str function” I’m implying that bool and str are functions. … Read more

What are hashable objects? – Python Morsels

By Trey Hunner Watch as video 03:25 What are hashable objects? Sets items must be hashable The elements in a set must be hashable. Strings and numbers are hashable: >>> colors = {“pink”, “blue”, “green”, “purple”} >>> choices = {10, 20, 30, 40} Tuples of strings and numbers are also hashable: >>> coordinates = {(0, … Read more