Question about idioms for clearing a list

Terry Reedy tjreedy at udel.edu
Wed Feb 8 14:23:17 EST 2006


"Magnus Lycka" <lycka at carmen.se> wrote in message 
news:dsd311$qlp$1 at wake.carmen.se...
> Statements and operators are really fundamental in Python. We
> don't support n = 1.add(2), since we have the '+' operator.

Actually, since the 2.2 union of type and new-style classes, which gave 
attributes to all types ....

>>> 1 .__add__(2)  # note space after 1
3
>>> 1.__add__(2)  # ambiguous, interpreter won't guess
SyntaxError: invalid syntax
>>> 1..__add__(2.)
3.0

I only see this as useful for making bound methods:

>>> inc = 1 .__add__
>>> inc(2)
3
>>> dub = 2 .__mul__
>>> dub(2)
4

which is certainly nicer than the older method of writing a 'makeoper' 
function that returned a nested function with either a default param or a 
closure.

Terry Jan Reedy






More information about the Python-list mailing list