Diabolical Python
06-03, 10:30–10:55 (Asia/Jerusalem), 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.

I was a tech lead and corporate innovator at the Ministry of Defense for 5 years; then a lecturer in Information Security at Tel-Aviv university; then joined the Android Framework team at Google London; and recently came back home to do AI for Magic Leap. A restless millennial all over the place, it would seem.
I've used Python for as long as I can remember, and always enjoyed its crazier side. When I finished my military service, Dave Beazley wished me to keep my Python code diabolical — and I've been doing so with great abandon ever since.