Reputation: 402
Consider this piece of code, wondering would it be possible to return a value before it gets updated.
class A:
def __init__(self):
self.n = 0
def get_next(self):
return self.n++ # Return its current value. after it gets returned, update n.
a = A()
a.get_next() # return 0
a.get_next() # return 1
Upvotes: 2
Views: 42
Reputation: 6760
This'll work:
def get_next(self):
old_value = self.n
self.n += 1
return old_value
Upvotes: 1