I have a doubt in performance and can't find how python behaves in each of these cases:
def something(object):
if object== 'something':
return 1
else:
return 0
Option 1:
def something_bigger(list, object):
total= 0
for item in list:
total+= something(object)
def something_bigger2(list, object):
total= 0
for item in list:
total-= something(object)
Option 2:
def something_bigger(list, object):
total= 0
something = something(object)
for item in list:
total+= something(object)
def something_bigger2(list, object):
total= 0
something = something(object)
for item in list:
total-= something(object)
The examples are simple and make no sense but my objective is to understand if python understands that `something()returns always the same and doesn't repeat the if endless times.
Option two, without understanding how python will do this, seems faster but on the other side I'm repeating my code!