Python logging configuration

In case you ever wondered how to correctly configure the python logging system, e.g. from files and configuration server, this might be a useful link, which I hadn’t see before: Logging Cookbook.

April 29, 2013 · updated April 30, 2021 · 1 min

Difference between comparisons with == and is in Python

In Python, two operators exist to perform equality comparisons: == and is. Often you can read instructions like “When comparing to None, always use is instead of ==”, but the real reason is not explained. However, looking at the language expression specification section of the Python manual you can find the difference: “The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.” This means the expressions “a is b” only returns True if both a and b point to the exact same object instance. Hence, you should always use is when you want to compare against specific instances. For the advice about using is to compare with None this means that there is only a single instance of None. The same is true also for Ellipsis. ...

April 8, 2013 · updated April 30, 2021 · 3 min

Evaluation of Default Arguments in Python

Today I stumbled upon a very subtle problem with default arguments in python. I noticed that loading a python module already instantiated one of my classes even though I could not find an installation of this class. In the end it turned out to be a default argument for a function: def foo(arg=MyClass()): pass As I am currently programming a lot in C++ this did not look suspicious to me. But from python’s point of view it absolutely makes sense that the default value for arg is already constructed at module load time and not only on a call to that functions. Moreover it is important to remember that this default argument is constructed only once for all calls to a function as stated here. ...

January 19, 2011 · updated April 30, 2021 · 1 min

PyGTK Segfault durch bereits geladenes GTK

Seit zwei Wochen bastel ich mich jetzt schon um einen mir unerklärlichen Segfault beim Importieren von PyGTK herum. Heute hab ich dann endlich mal etwas Zeit gehabt das ganze genauer unter die Lupe zu nehmen. Das Modul in meinem Projekt, das die GUI-Funktionalität bereitstellt, enthält folgende Imports: import pygtk pygtk.require('2.0') import gtk Bis zur require-Zeile ist das alles kein Problem, doch import gtk hat in manchen Situationen den Segfault ausgelöst. Seit heute weiß ich, dass dieser Segfault auftritt, wenn vorher bereits ein Modul geladen wurde, das gegen GTK gelinkt wurde, in meinem Projekt z.B. der loader: ...

June 17, 2008 · updated April 30, 2021 · 1 min

Style Guide für Python

Style Guides für Java und C/C++ gibt es ja zu Hauf, für Python war mir aber bisher noch kein explizit aufgeschriebener aufgefallen - zumindest bis gestern: PEP 8 – Style Guide for Python

June 15, 2008 · updated April 30, 2021 · 1 min