[Python-ideas] Adding __getter__ to compliment __iter__.

Stephen J. Turnbull stephen at xemacs.org
Mon Jul 22 08:37:14 CEST 2013


Ron Adam writes:

 > '+=' is a additive/append type of operation.

Ouch.  When '+=' is additive, it's a binary operator on a single set.
As such, it should take a sequence on both sides (assuming for the
moment on wants to use it on sequences at all), and thus would
correspond to concatenation (ie, list.extend()).  list.append() and
set.add() take things of different types, a container on the left and
an object on the right (at least, if you made them into assignment
operators, that's what they'd do).

 > A much less used symbol is __lshift__ , '<<'.

But *shift is precisely the opposite: it takes a container on the left
(eg, a word thought of as a bitstream) and a shifter (usually a
number) on the right.  I guess that's why Stroustrup (or whoever)
chose left-shift for his iostream operators.  So my intuition is
precisely the opposite of yours as far as choice of spelling goes.
For strings or lists, "<<=" would append an element, and "+=" would
extend using a list.

In any case the discussion is moot: the operator "+=" already has the
definition I find more intuitive for the built-in sequence types.

 > And interesting alternative possibility is a concatenation_comprehension. 
 > Or cat_comp for short.
 > 
 >      >>> ("One " << "Two " << "Three!")
 >      'One Two Three!'

That's not a comprehension.  A comprehension aggregates (and possibly
transforms and filters) the elements of a container.  Here the strings
are elements listed explicitly; it's just an ordinary expression as
far as anybody who doesn't know how magic "<<" is would know.







More information about the Python-ideas mailing list