Skip to Content
0 likes
Python Dictionary Comprehensions: When They Help and When They Hurt Level: Intermediate

A dictionary comprehension builds a dictionary in one line. Good for transforming, filtering and building lookups. Bad for nested logic, side effects and anything that needs error handling. Shorter code is not always better code.

0 likes
Python Decorators Explained With One Real Example Level: Advanced

A decorator is just a function that wraps another function. Build a real timing decorator step by step: functions as values, the wrapper, the @ syntax, and functools.wraps. After that, the syntax stops looking like magic.

0 likes
Python Generators: Handle Big Files Without Running Out of Memory Level: Advanced

Reading a big file into a list can kill your program. A generator handles one line at a time. See the memory difference measured on a real file, learn what yield does, and chain small generators into a pipeline.

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
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 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
Writing Your Own Python Context Manager, With a Class or contextlib Level: Advanced

Write a context manager when something must be undone no matter how the block ends. Build one as a class and with @contextmanager, keep yield inside try/finally, and don't swallow errors by accident.