»Diabolical Python«
2019-06-03, 10:30–10:55, Main Hall

A collection of 3 short stories, each one crazier than the last: from tracing and metaprogramming to patching frame objects and rewriting bytecode.

Python is an incredibly powerful language, but just how powerful it is can wrinkle your brain. 3 simple enough ideas — ordered class attributes, c-like enums, and methods with an implicit self — are implemented with ever crazier (and ever more powerful) tricks, that shouldn't be used under any circumstances (and are therefore completely useless. In fact, you probably shouldn't come).

Seriously though, I'll let the code speak for itself:

@ordered()
class A:
    x = 1
    y = 2
    z = 3
assert A._order == ['x', 'y', 'z'] # In Python 2.7, too!

@cenum()
class A:
    a
    b
    c = 10
    d
assert A.a == 0
assert A.b == 1
assert A.c == 10
assert A.d == 11

@selfless
class A:
    def __init__(x):
        self.x = x
    def f():
        return self.x + 1
a = A(1)
assert a.x == 1
assert a.f() == 2

If you want to find out how this is possible — and learn a few things about tracers, metaclasses, frames and code objects along the way — this talk is for you.