Brython - Python in the browser
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Dec 20 21:05:03 EST 2012
On Thu, 20 Dec 2012 18:59:39 -0500, Terry Reedy wrote:
>> On Thu, Dec 20, 2012 at 8:37 PM, Pierre Quentel
>> <pierre.quentel at gmail.com> wrote:
>>> I'm afraid I am going to disagree. The document is a tree structure,
>>> and today Python doesn't have a syntax for easily manipulating trees.
>
> What Python does have is 11 versions of the augmented assignment
> statement: +=, -=, *=, /=, //=, %=, **=, >>=, <<=, &=, ^=, |=. Moreover,
> these are *intended* to be implemented in place, by mutation, for
> mutable objects, with possibly class-specific meanings.
I don't believe that is the case. The problem is that augmented
assignment that mutates can be rather surprising to anyone who expects
"a += b" to be a short cut for "a = a + b".
py> a = [1, 2, 3]; b = [99]; another = a
py> a = a + b
py> print(a, another) # What I expect.
[1, 2, 3, 99] [1, 2, 3]
py> a = [1, 2, 3]; b = [99]; another = a
py> a += b
py> print(a, another) # Surprise!
[1, 2, 3, 99] [1, 2, 3, 99]
Whichever behaviour you pick, you're going to surprise somebody. So I
wouldn't say that mutate in place is *intended* or preferred in any way,
only that it is *allowed* as an optimization if the class designer
prefers so.
One might even have a class where (say) __iadd__ is defined but __add__
is not.
[...]
> <= is a comparison expression operator, which is completely different.
<= is a comparison operator for ints, floats, strings, lists, ... but not
necessarily for *everything*. That's the beauty and horror of operator
overloading. Any operator can mean anything.
If it were intended to only return a flag, then 1) Python would enforce
that rule, and 2) the numpy people would be most upset.
I have no opinion on the usefulness or sensibility of using <= as an in-
place mutator method in this context, but I will say that if I were
designing my own mini-DSL, I would not hesitate to give "comparison
operators" some other meaning. Syntax should be judged in the context of
the language you are using, not some other language. If you are using a
DSL, then normal Python rules don't necessarily apply. <= in particular
looks just like a left-pointing arrow and is an obvious candidate for
overloading.
--
Steven
More information about the Python-list
mailing list