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
  • 143
  • 4

3 Answers3

3

Generator example:

def abc():
    yield 5
    yield 6
gen = abc()
var = next(gen) # var = 5
###
var = next(gen) # var = 6
Marat
  • 13,099
  • 2
  • 37
  • 44
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
  • 4,174
  • 4
  • 28
  • 56
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
  • 234
  • 2
  • 9