1

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
Ri Di
  • 163
  • 5

3 Answers3

3

Generator example:

def abc():
    yield 5
    yield 6
gen = abc()
var = next(gen) # var = 5
###
var = next(gen) # var = 6
Marat
  • 15,215
  • 2
  • 39
  • 48
2

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)
user32882
  • 5,094
  • 5
  • 43
  • 82
0

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


Nadavo
  • 250
  • 2
  • 9