Q: Python returning function results What is better with respect to speed and code optimization? Performing a computation, storing the result reference locally and returning the reference like so:
def aplusb(a,b):
    result = a + b
    return result

or return whatever the computation returns directly to the caller:
def aplusb(a,b):
    return a + b


A: The first executes two more bytecodes, one for storing the result, the other for retrieving it again:
>>> def aplusb_local(a, b):
...     result = a + b
...     return result
...
>>> def aplusb_return(a, b):
...     return a + b
...
>>> import dis
>>> dis.dis(aplusb_local)
  2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST                1 (b)
              4 BINARY_ADD
              6 STORE_FAST               2 (result)

  3           8 LOAD_FAST                2 (result)
             10 RETURN_VALUE    
>>> dis.dis(aplusb_return)
  2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST                1 (b)
              4 BINARY_ADD
              6 RETURN_VALUE

You also need a little more local variable space in the function frame object:
>>> aplusb_local.__code__.co_nlocals
3
>>> aplusb_return.__code__.co_nlocals
2

The differences here are extremely marginal; a 0.038 second difference on 10 million calls on my Macbook Pro:
>>> from timeit import timeit
>>> timeit('aplusb(42, 81)', 'from __main__ import aplusb_local as aplusb', number=10**7)
1.0834476329619065
>>> timeit('aplusb(42, 81)', 'from __main__ import aplusb_return as aplusb', number=10**7)
1.045598166063428

This is not something you should be optimising for.
Optimise for readability instead.
