Defining new infix operators in Python

This is an absolutely blinding hack. See if you can guess how it's done before looking at the recipe...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Andrei Alexandrescu described

Andrei Alexandrescu described something very similar in C++ a while ago in a post to comp.lang.c++.moderated.

Cute

Whenever I see this sort of thing I have a hard time believing some of the same people argue against real macro sytems...

OK, Here I Go...

Ehud Lamm: Whenever I see this sort of thing I have a hard time believing some of the same people argue against real macro sytems...

I've been trying to avoid getting into more political wars, but this is exactly the problem that I see with those who argue against formalism: ultimately they want the ability to do what I call "programming by stunt." It's really just a sub-strain of cargo-cult programming. They want the bennies of macros or operator overloading without having to understand how such features interact with other language features or, worse, with such features interacting with other language features in an incoherent way.

Poetry

I'm not following how "programming by stunt" -- i.e. writing trickily clever programs -- is like "cargo-cult programming" -- i.e. doing things in a particular way without understanding why. In fact, they seem completely unreleated, since the former, when it depends on abusing language features, often demands a deep understanding of the language's quirks.

Sunny optimist that I am, I prefer to think of it as a kind of poetry, and doing macro-like things with a built-in macro system is, as someone once said of writing poetry without meter, like playing tennis with the net down. And just like poetry, people are writing these things for amusement, not for hard-headed engineering or lofty intellectual reasons.

Lofty and Wendy Join The Crew

And just like poetry, people are writing these things for amusement, not for hard-headed engineering or lofty intellectual reasons.

People do sometimes write poetry for lofty intellectual reasons, too...

Reminds me of 1999.

I used to do such tricks in C++ back then :)