Python should do more work for dict.get() :
get is an attribute, so Python should look at this and then bind the handle found to the dictionary instance.() is a call, so the current frame must be pushed onto the stack, the call must be made, then the frame must be taken out of the stack again to continue.
The notation [...] used with dict does not require a separate step or attribute step, click and place the frame.
You can see the difference when you use the Python dis bytecode disassembler dis :
>>> import dis >>> dis.dis(compile('d[key]', '', 'eval')) 1 0 LOAD_NAME 0 (d) 3 LOAD_NAME 1 (key) 6 BINARY_SUBSCR 7 RETURN_VALUE >>> dis.dis(compile('d.get(key)', '', 'eval')) 1 0 LOAD_NAME 0 (d) 3 LOAD_ATTR 1 (get) 6 LOAD_NAME 2 (key) 9 CALL_FUNCTION 1 12 RETURN_VALUE
therefore, the expression d[key] should only execute the operation code BINARY_SUBSCR , and d.get(key) adds the operation code LOAD_ATTR . CALL_FUNCTION much more expensive than BINARY_SUBSCR for the built-in type (custom types with __getitem__ methods still end up calling the function).
If most of your keys exist in the dictionary, you can use try...except KeyError to handle missing keys:
try: return mydict['name'] except KeyError: return None
Exception handling is cheap if there are no exceptions.
source share