Skip to Content
193 results
0 likes
Python Closures: Building a Counter Without a Global Variable Level: Advanced

A closure remembers variables from the function that created it. Build separate counters with no globals, fix the UnboundLocalError with nonlocal, avoid the lambda-in-a-loop trap, and know when a class is clearer.

0 likes
Finding the Slow Part of Your Python Code With cProfile Level: Advanced

Don't guess which part is slow. Run cProfile, read cumtime and tottime the right way, fix the one line that matters, and measure again. In the example, the obvious suspect isn't the problem.

0 likes
Writing a Custom Iterator Class in Python With __iter__ and __next__ Level: Advanced

A for loop calls iter() once and next() until StopIteration. Write __iter__ and __next__ yourself, avoid the iterator that only works once, and know when a generator in __iter__ does the same job in fewer lines.

0 likes
Python dataclasses: A Class Without Writing __init__ by Hand Level: Intermediate

If a class mostly holds data, @dataclass writes __init__, a readable printout and equality for you. Use field(default_factory=list) for lists, __post_init__ for checks and frozen=True for values that must not change.

0 likes
Fixing ModuleNotFoundError in Python, One Cause at a Time Level: Intermediate

ModuleNotFoundError has a handful of causes: the wrong Python, an inactive virtual environment, the wrong folder, or a file that shadows a module. Check them in order instead of reinstalling.

0 likes
Python Function Arguments: Defaults, Keywords and the Mutable Default Trap Level: Beginner

Defaults make functions easier to call and keywords make calls easier to read. A list as a default value is created once and shared by every call. Here's why, and the one-line fix.

0 likes
Python if, elif and else: Writing Conditions That Don't Overlap Level: Beginner

An if/elif chain stops at the first true condition. Put the widest one first and the others never run, with no error. Order your conditions, test the edges, and make else mean what you think.

0 likes
Python if __name__ == "__main__": What It Does and When You Need It Level: Intermediate

Importing a file runs it. if __name__ == "__main__" keeps a file's script part from running when another file imports it. Use it with a main() function, and know the cases where you don't need it.

0 likes
Python Is Not Recognized? Fixing PATH on Windows, Mac and Linux Level: Beginner

Python is probably installed. Your terminal just can't find it. Check the three possible causes in order, fix the right one on Windows, Mac or Linux, and confirm which Python you're really running.

0 likes
Python Division and Rounding: /, //, % and round() Without Surprises Level: Beginner

/, //, % and round() each do exactly what they say, and that's not always what school math taught you. Negative numbers, halves and money are where the surprises are, and where Decimal comes in.

0 likes
Python logging: Replacing print() Before It Gets Out of Hand Level: Intermediate

print() is for the output a person asked for. logging is for messages about the program: levels you can turn down, a file with times, and tracebacks for errors. Set it up once in main() and use a logger per module.

0 likes
Adding mypy to an Existing Python Project Without 500 Errors on Day One Level: Advanced

Start mypy with its defaults, fix what it finds, turn on check_untyped_defs, then make one module at a time strict. Every function you annotate checks every place that calls it, so start with the busiest ones.