A Neighborhood of Infinity: Overloading Python list comprehension

A Neighborhood of Infinity google_ad_section_start(name=default) Saturday, March 17, 2012 Overloading Python list comprehension Introduction Python is very flexible in the way it allows you to overload various features of its syntax. For example most of the binary operators can be overloaded. But one part of the syntax that can't be overloaded is list comprehension ie. expressions like [f(x) for x in y] . What might it mean to overload this notation? Let's consider something simpler first, overloading the binary operator + . The expression a+b is interpreted as a.__add__(b) if a is of class type. So overloading + means nothing more than writing a function. So if we can rewrite list comprehensions in terms of a function (or functions) then we can overload the notation by providing alternative definitions for those functions. Python doesn't provide a facili...

Linked on 2014-11-12 23:48:41 | Similar Links