I want to return variable from function using return and after that call same function, but resume from return. It this possible? Example:
def abc():
return 5
return 6
var = abc() # var = 5
###
var = abc() # var = 6
I want to return variable from function using return and after that call same function, but resume from return. It this possible? Example:
def abc():
return 5
return 6
var = abc() # var = 5
###
var = abc() # var = 6
Generator example:
def abc():
yield 5
yield 6
gen = abc()
var = next(gen) # var = 5
###
var = next(gen) # var = 6
I think you're looking for a generator:
def abc():
yield 5
yield 6
gen = abc()
var = next(gen)
print(var)
var = next(gen)
print(var)
returning a value in python terminates the function's process. the whole point is that it is returning exactly one value, once being done. maybe try to solve your problem by returning a stack- with as many values as you need,
def abc():
a = []
a.append(6)
a.append(5)
return a
var = abc().pop # var = 5
###
var = abc().pop # var = 6