Find Related products on Amazon

Shop on Amazon

Frankenstein's `__init__`

Published on: 2025-08-18 12:32:29

Frankenstein’s __init__ Inspired by a recent post about the woes of __init__ methods in Python, I thought I’d share the untold story of the absolute craziest __init__ I’ve come across in a production codebase. It all started when I tried to add a failing test to a Python service. The test was indeed failing, but every now and then it would fail on something unexpected. The Evidence of the Test After some minimization, this was the test I had: def test_foobar (): f = FooBarWidget () with contextlib . closing ( f ): assert False which sometimes failed with this error: self = < foobar . FooBarWidget object at 0x10512ed80 > def close ( self ): > if self . should_exit is False : E AttributeError : 'FooBarWidget' object has no attribute 'should_exit' foo . py : 28 : AttributeError And not (only) the expected AssertionError . Searching for self.should_exit = yielded FooWidget.__init__ in foo.py : class AbstractWidget : def __init__ ( self ): self . config = Path ( "config.json" ) . ... Read full article.