4 ms·
return {**old, 'foo': 'bar'} # Old way return dict(old, foo='bar') Not much difference if you ask me.
by est 9y ago
return {**old, 'foo': 'bar'}
# Old way
return dict(old, foo='bar')
Not much difference if you ask me.
- delaaxe 9y agoI've been practicing Python for a while and didn't even know about this. In my code style, I try to completely avoid the "dict" keyword and exclusively use dict literal notation.
- orf 9y agoIt's twice as slow, doesn't work with more than one dictionary, you can't easily control the merge prescience and you can't (easily) use expressions/variables in the keys: {**x, 'fo'+'o': 'bar', **y}
- rburhum 9y agowhy wouldn't you be able to just keep the same syntax and make it twice as fast in a newer implementation?
- willtim 9y agoSurely a more intuitive syntax would be: X ++ {'fo'+'o': 'bar'} ++ y Where '++' here means dictionary union, the choice of symbol is not relevant.
- orf 9y agoNot really, and it's not getting most pythonic way of doing things IMO. With the syntax above (I think) you can work with any iterable, whereas with a dictionary union operator you'd have to define it on every class you'd want to use, and you'd be out of luck with generators.
- Doxin 9y agoreturn dict(x, **{'fo'+'o': 'bar'}, **y) The new syntax is some mild syntactic sugar. (Which isn't a bad thing IMO)
- orf 9y agoOn the face of it, sure, but really it's some new bytecode that gets rid of the limitations of using kwargs like that, namely it's slow, impossible to really optimize and doesn't support duplicate keys.
- masklinn 9y agoExcept that does not necessarily work with non-string keys (it'd depend on version and implementation IIRC). The expanded unpacking works in all cases.
- singingfish 9y agoAnd people say perl is unreadable ;)
- kamaal 9y agoThis is the most unreadable code I've seen so far. Just skip all this and use Perl instead. You can write far more idiomatic, succinct readable code in Perl than you can in Python. The whole point of Python is to not write code this way.
- vgy7ujm 9y agoThis is what I have been saying for a long time. Readability in Python is an illusion.
- Sean1708 9y agoDid you try your example? It never worked in Python2 and it doesn't work in Python3 by design. Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> x = {'foo': 5, 'bar': 6} >>> y = {'foo': 7, 'baz': 9} >>> dict(x, **{'fo'+'o': 'bar'}, **y) File "<stdin>", line 1 dict(x, **{'fo'+'o': 'bar'}, **y) ^ SyntaxError: invalid syntax Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x = {'foo': 5, 'bar': 6} >>> y = {'foo': 7, 'baz': 9} >>> dict(x, **{'fo'+'o': 'bar'}, **y) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: type object got multiple values for keyword argument 'foo' >>> {**x, 'fo'+'o': 'bar', **y} {'foo': 7, 'baz': 9, 'bar': 6}
- agumonkey 9y agoI'm allergic to the { ... } thing. And I like terse and cryptic, but for some reason I find it the less attractive syntactical trick of all python3 (that I know). dict(a, ... ,b) feels cleaner.