[Python-ideas] Why operators are useful

Jimmy Girardet ijkl at netc.fr
Mon Mar 18 11:07:11 EDT 2019


Please let me share my story of non experienced python programmer.

Last year I wanted to merge three dicts  for config stuff.

I found very quickly the answer : a = {**b, **c, **d}

Sadly I was working on python 3.3 and that was nos possible to use this
syntax. I don't remember what I did next : use chain,; ChainMap,  some
comprehension or some a.update() but I was missing the "upacking syntax".

The syntax {**b,**c} wasn't hard to remember. That wasn't something
known by mathematician, experienced programmers or some artist at the
first look maybe. But It's a clear syntax easy to remember. Easy because
two arterisk `**` in python is a well known syntax due to `**kwargs` in
many functions. And easy because at the end it's idiomatic.

Many things are not straightforward in python depending where you come
from :

if __name__ == '__main__':  # Ugly

len(collection) et not collection.len() # Ugly depending your
programming background

item  in collection instead of collection.contains(i) # same thing.

list/dict comprehensions...

At the end, only a few things are straightforward at the beginning, so
d1+d2 fails isn't a big deal since you will easy remember after a quick
initial search the idiom {**d1,***d2}


Le 18/03/2019 à 15:12, Antoine Pitrou a écrit :
> On Mon, 18 Mar 2019 14:06:53 +0000
> Rhodri James <rhodri at kynesim.co.uk> wrote:
>> On 16/03/2019 12:01, Gustavo Carneiro wrote:
>>> Already been said, but might have been forgotten, but the new proposed
>>> syntax:
>>>      new = a + b
>>> has to compete with the already existing syntax:
>>>      new = {**a, **b}
>> That's easy.  Whether it's spelt with "+" or "|" or pretty much anything 
>> else, the operator version is clearer and cleaner.  "{**a, **b}" is a 
>> combination of operators and literal (display) syntax, and following 
>> Guido's reasoning that makes it inherently harder to interpret.  It's 
>> also ugly IMHO, but that's me.
> The question is whether it's too hard or ugly for the use cases.  In
> other words: where are the use cases where it's frequent enough to
> merge dicts that a nicer syntax is required?
> (also, don't forget you can still use the copy() + update() method)
> Regards
> Antoine.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

More information about the Python-ideas mailing list