Yes.
If you use CPython and strings as keys, then yes. Gil in CPython ensures that only one thread executes the bytecode at a time, and the key is set to dict in the same STORE_SUBSCR operation STORE_SUBSCR . If you are not using CPython or using a key with custom methods __hash__ , __eq__ or __cmp__ , all bets are disabled. If I had a soapbox, I would jump on it and warn you of evil, relying on implementation details like this for the sake of correctness. It is more pythonic of you to write something that works only for the case and in the environment where it will be used, since otherwise it could be considered as premature optimization. Enjoy your working code!
>>> from dis import dis >>> dis(compile('output = defaultdict(dict); output["water"] = "H2O"', 'example', 'exec')) 1 0 LOAD_NAME 0 (defaultdict) 3 LOAD_NAME 1 (dict) 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 9 STORE_NAME 2 (output) 12 LOAD_CONST 0 ('H2O') 15 LOAD_NAME 2 (output) 18 LOAD_CONST 1 ('water') 21 STORE_SUBSCR 22 LOAD_CONST 2 (None) 25 RETURN_VALUE
This has been discussed elsewhere .
source share