1

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?

Jab
  • 26,853
  • 21
  • 75
  • 114
  • "depending on the implementation, it would nullify having to hold the value r in memory" - you really shouldn't try to optimize based on speculation like that. As a matter of fact, no, it would not prevent having to hold the value in memory, even if syntax like this existed. (Also, in every language I know where `return x -= y` syntax exists, the value returned is the value after subtraction, not before.) – user2357112 Nov 23 '18 at 06:55
  • Also, the method is `__next__` on Python 3. – user2357112 Nov 23 '18 at 06:57
  • Basically, why can’t `val += i` return it’s altered value then? Seems to me this should be viable. – Jab Nov 23 '18 at 06:59
  • One-liners are overrated. In Python, an assignment is not an expression, so you can't do that sort of thing. This was a deliberate design decision to prevent the hard-to-read nested assignments that C allows. – PM 2Ring Nov 23 '18 at 07:16
  • But, but I like those nested assignments. Shorter the better. Either way it’s nothing I’m hung up on I just prefer shorter code on simpler tasks – Jab Nov 23 '18 at 07:22

2 Answers2

1
return foobar -= 1

or

>>> a = 3
>>> b = (a += 1)
  File "<stdin>", line 1
    b = (a += 1)
            ^
SyntaxError: invalid syntax

is not possible in Python.

Although the first solution needs to store one more variable for this timestep (or do one operation more), to cite the Python Zen: Readability counts.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • I understand that `return foo-=1` isn’t possible, I’m asking if there is a way to accomplish this without being as verbose as my first example. I just feel `return foo-=1` is easily readable but less verbose than the later – Jab Nov 23 '18 at 06:48
1

It's because -= modifies the variable, and do any thing to that, (like now you returned that) will raise errors.

Demo:

>>> a=3
>>> a+(a+=1)
SyntaxError: invalid syntax
>>> # also to show that it does modify the variable:
>>> a=3
>>> a+=1
>>> a
4
>>> 

Update:

You do a two-liner:

def f(a):
    if a<=0:raise StopIteration
        a-=1;return a
U13-Forward
  • 69,221
  • 14
  • 89
  • 114