On 17.11.2016 11:16, Mikhail V wrote:
> Citation from
> http://legacy.python.org/dev/peps/pep-0203/
> 
> """
> Expressions of the form
> 
>         <x> = <x> <operator> <y>
> 
>     are common enough in those languages to make the extra syntax
>     worthwhile, and Python does not have significantly fewer of those
>     expressions. Quite the opposite, in fact, since in Python you can
>     also concatenate lists with a binary operator, something that is
>     done quite frequently. Writing the above expression as
> 
>         <x> <operator>= <y>
> 
> ->    __is both more readable__ and less error prone, because it is
>     instantly obvious to the reader that it is <x> that is being
>     changed, and not <x> that is being replaced by something almost,
>     but not quite, entirely unlike <x>.
> 
> """
> 
> Aha, sure, if "readable" == "suits C programmers".
> And it's surely not, which in some sence also means that it *more*
> error prone, since I cannot notice possible error so easy.

The above follows the general principle in Python that
"explicit is better than implicit":

1. "a = a + 1" means (in Python): add 1 to a (via the
.__add__() method) and bind the new value to "a".

2. "a += 1" means (in Python): let the object a add 1 to itself
(via the .__iadd__() method) and bind the new value to "a".

In the first variant, a may well result in a new object being
created, even changing the object type.

In the second variant, it is immediately clear that the intent
is for a to do something to itself. It is much less likely to
result in a new object type (even though it may still result in
a new object being created). More common is to have a manipulate
itself without even changing the object.

Examples:
---------

>>> l = [1,2,3]
>>> id(l)
140431507775856
>>> l = l + [4]
>>> id(l)
140431507776864

Here, a new list object was created.

>>> l += [5]
>>> id(l)
140431507776864
>>>

Here, the object itself was manipulated without creating
a copy; and that's what you'd typically expect from an in-place
operation on mutable objects.

The situation is different with immutable objects:

>>> a = 1
>>> id(a)
27693400
>>> a = a + 1
>>> id(a)
27693376
>>> a += 1
>>> id(a)
27693352

In both cases, you get new objects (but the object type
remains the same).

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Nov 17 2016)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to