Björn Ricks A personal Blog.

Mon 04 March 2024

Understanding Context Manager and its Syntastic Sugar

The Context Manager is one of my favorite features in Python. It can be used for all kind of interesting things around blocks of code. This article gives a background about the origins of the context manager, which problem it solves and how to use it.

  • Namespaces
  • Default Namespace
  • Serialization
  • Origins
  • Context Manager Protocol
  • The With Statement as Syntastic Sugar
  • Implementing a Context Manager using contextlib
  • Capture the Call Flow
  • Example Context Managers
    • Example 1 - Redirect Stdout
    • Example 2 - Suppress Exceptions
    • Example 3 - Add a directory to the Python module search path temporarily
    • Example 4 - Print Prefix
  • Links
Read more
Thu 08 February 2024

xml.etree.ElementTree vs. lxml.etree

When working with XML and Python I often start with just using xml.etree.ElementTree from Python’s standard library. It comes with Python and is sufficient for most projects. But with quite big XML documents (> 1 GiB) parsing becomes slow and lxml.etree shines.

Read more
Fri 10 November 2023

What's new in Python 3

Quick overview about the most important additions in a Python release

  • Python 3.12
    • Typing
    • f-strings
    • Pathlib
    • Itertools
  • Python 3.11
    • Features
    • Typing
    • Enum
    • AsyncIO
  • Python 3.10
    • Features
    • Typing
    • Dataclasses
    • AsyncIO
  • Python 3.9
    • Features
    • Typing
    • AsyncIO
Read more
Contents © 2025 Björn Ricks - Powered by Pelican