|
|
Subscribe / Log in / New account

Python dictionary "addition" and "subtraction"

Python dictionary "addition" and "subtraction"

Posted Mar 14, 2019 9:27 UTC (Thu) by NRArnot (subscriber, #3033)
Parent article: Python dictionary "addition" and "subtraction"

I'm very lukewarm. What is wrong with using .copy() and .update() methods? "Explicit is better than implicit" and methods are certainly more explicit.

But if there is support, I'd argue strongly against using + and - operators. This is far more likely to lead to accidents than using |. This latter operator is far less frequently used, and seeing it always alerts one to the fact that something out of the ordinary is going on. Also one can visualize a set as a dict where all values are the same and irrelevant, such as True. In fact back before we had sets, that's what we used to do. So extending the set | operator to apply similarly to dicts makes reasonable sense.

Subtraction I like even less. Non-commutativity I can live with, but subtraction completely ignores the values in the dict being "Subtracted". A much better idea would be a dict.subtract( other) method.


to post comments

Python dictionary "addition" and "subtraction"

Posted Mar 15, 2019 19:00 UTC (Fri) by k8to (guest, #15413) [Link]

Sometimes I think language projects just try to incorporate change because solving problems means making changes in the minds of most people, while accepting the status quo as fine can feel like not solving a problem.

I find the methods quite clear and provide good hints about what sort of data type we're working with here anyway for code that doesn't bother to provide that information in an easily accessible way. Even with the relatively constrained set of semantics for built-in types in basic python, I often find production code where the developers haven't really understood the provided toolkit. I'm not a fan of adding semantic synonyms that offer no specific utility when in practice it's clear that programmers are otten already operating beyond their understanding of their tools.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds