Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

dict.merge(d, ...) and dict.diff(d, ...) are more expressive and have a cleaner semantic.

overloading arithmetic ops for string, list or dict ops might only look elegant at first sight, but discrimination needs to be done at runtime, slowing down the most important arithmetic ops, and do not help much the casual code reader. It also cannot be used in normal python code as older python will fail, only in special internal code.

normal method names can be provided by external modules, so they are backwards compatible and will find more widely adoption.



Teacher here.

All my students eventually try {} + {}.

I'll bet on it to be the most intuitive.


Students are supposed to set things on fire and find various nifty features for learning's sake. But with experience - and the discovery that various such features largely overlap (eg. + and .extend()), one tends to grow out of that stance and become wiser.


It's intuitive to try, but it's not obvious what it actually does when keys overlap.


It’s obvious what you want done, and thankfully that is what’s going to be done.


the problem with overloading add is that add results in more than merge. merge is add minus the duplicate keys. students shouldn't learn wrong semantics.

{1:,2:} + {1:,3:} = {1:,2:,3:}


There is a conter proposal to use classmethod like this from another core developer.


with an object method you could return the changed dict (ref semantics, much faster) and as class methodcall you would return a copy, as with +.

d.merge(d1) vs dict.merge(d, d1)

So have the best of both worlds, and backwards compatible.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: