def size(number):
if number<100:
return Small()
if number<1000:
return Medium()
return Big()
def size1(number):
if number<100:
return Small()
elif number<1000:
return Medium()
else:
return Big()
Regarding coding style:
I often use the former when the following blocks, or the last block is a large chunk of code. It seems to help readability.
I generally use the latter when the various blocks have a common concept running through them (as in the case above). The common indentation helps to communicate their relationship.
Are there any differences between these two worth noting (esp. performance wise)?
Style-wise, I find the second example easier on the eye.
In all other respects, there is no difference. The two functions compile to identical bytecodes:
(To be 100% accurate, the second version has an implicit
return Noneat the end. However, since this code is not reachable, it won’t affect performance.)