Well honestly no easy way for me to lamens the title of this question, basically is there a way to modify a return value for a function while also returning it in the same line.
Example - custom implementation of an iterable class
I’d like to replace this:
def __next__(self):
if self.count <= 0:
raise StopIteration
r = self.count
self.count -= 1
return r
With this:
def __next__(self):
if self.count <= 0:
raise StopIteration
return self.count -= 1
Honestly, I know this may seem frivolous (which it may be) but I’m only this because I’m a fan of one-liners and this boils down to making even a process as simple as this more logically readable; plus, depending on the implementation, it would nullify having to hold the value r
in memory (I know I know, removing the need for r
has no significant gain but hey I’m only asking if this is possible).
I know I’ve only given one example but this happens to be the only case I can think of that something like this Would be needed. Python is a wonderful language full of many special things like +=
being a “wrapper” of __iadd__
my thing is am I missing something? Or is this possible... and why must it be used as a single line and not in conjunction with a return statement as it doesn’t return its altered value?