Is it possible to return a value while still modifying it?












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?










share|improve this question
























  • "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 6:55












  • Also, the method is __next__ on Python 3.
    – user2357112
    Nov 23 '18 at 6:57










  • Basically, why can’t val += i return it’s altered value then? Seems to me this should be viable.
    – Jaba
    Nov 23 '18 at 6: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 7: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
    – Jaba
    Nov 23 '18 at 7:22
















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?










share|improve this question
























  • "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 6:55












  • Also, the method is __next__ on Python 3.
    – user2357112
    Nov 23 '18 at 6:57










  • Basically, why can’t val += i return it’s altered value then? Seems to me this should be viable.
    – Jaba
    Nov 23 '18 at 6: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 7: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
    – Jaba
    Nov 23 '18 at 7:22














1












1








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?










share|improve this question















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?







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 7:00

























asked Nov 23 '18 at 6:29









Jaba

6,831175293




6,831175293












  • "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 6:55












  • Also, the method is __next__ on Python 3.
    – user2357112
    Nov 23 '18 at 6:57










  • Basically, why can’t val += i return it’s altered value then? Seems to me this should be viable.
    – Jaba
    Nov 23 '18 at 6: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 7: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
    – Jaba
    Nov 23 '18 at 7:22


















  • "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 6:55












  • Also, the method is __next__ on Python 3.
    – user2357112
    Nov 23 '18 at 6:57










  • Basically, why can’t val += i return it’s altered value then? Seems to me this should be viable.
    – Jaba
    Nov 23 '18 at 6: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 7: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
    – Jaba
    Nov 23 '18 at 7:22
















"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 6:55






"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 6:55














Also, the method is __next__ on Python 3.
– user2357112
Nov 23 '18 at 6:57




Also, the method is __next__ on Python 3.
– user2357112
Nov 23 '18 at 6:57












Basically, why can’t val += i return it’s altered value then? Seems to me this should be viable.
– Jaba
Nov 23 '18 at 6:59




Basically, why can’t val += i return it’s altered value then? Seems to me this should be viable.
– Jaba
Nov 23 '18 at 6: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 7:16




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 7: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
– Jaba
Nov 23 '18 at 7:22




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
– Jaba
Nov 23 '18 at 7:22












2 Answers
2






active

oldest

votes


















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





share|improve this answer























  • @Jaba i edited mine, Btw, you received my email?
    – U9-Forward
    Nov 23 '18 at 6:54






  • 1




    Yes, thanks! I will respond later
    – Jaba
    Nov 23 '18 at 7:01






  • 1




    Ok, now that’s more my liking, look at that simplicity
    – Jaba
    Nov 23 '18 at 7:19










  • @Jaba Happy to help, :-), 😊😊😊, (just got back to stackoverflow)
    – U9-Forward
    Nov 24 '18 at 23:39










  • @Jaba Haven't so a reply yet as promised :-), tho you can answer anytime, lol :D, 😜
    – U9-Forward
    Nov 28 '18 at 4:05



















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.






share|improve this answer





















  • 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
    – Jaba
    Nov 23 '18 at 6:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441591%2fis-it-possible-to-return-a-value-while-still-modifying-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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





share|improve this answer























  • @Jaba i edited mine, Btw, you received my email?
    – U9-Forward
    Nov 23 '18 at 6:54






  • 1




    Yes, thanks! I will respond later
    – Jaba
    Nov 23 '18 at 7:01






  • 1




    Ok, now that’s more my liking, look at that simplicity
    – Jaba
    Nov 23 '18 at 7:19










  • @Jaba Happy to help, :-), 😊😊😊, (just got back to stackoverflow)
    – U9-Forward
    Nov 24 '18 at 23:39










  • @Jaba Haven't so a reply yet as promised :-), tho you can answer anytime, lol :D, 😜
    – U9-Forward
    Nov 28 '18 at 4:05
















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





share|improve this answer























  • @Jaba i edited mine, Btw, you received my email?
    – U9-Forward
    Nov 23 '18 at 6:54






  • 1




    Yes, thanks! I will respond later
    – Jaba
    Nov 23 '18 at 7:01






  • 1




    Ok, now that’s more my liking, look at that simplicity
    – Jaba
    Nov 23 '18 at 7:19










  • @Jaba Happy to help, :-), 😊😊😊, (just got back to stackoverflow)
    – U9-Forward
    Nov 24 '18 at 23:39










  • @Jaba Haven't so a reply yet as promised :-), tho you can answer anytime, lol :D, 😜
    – U9-Forward
    Nov 28 '18 at 4:05














1












1








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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 6:54

























answered Nov 23 '18 at 6:43









U9-Forward

13k21137




13k21137












  • @Jaba i edited mine, Btw, you received my email?
    – U9-Forward
    Nov 23 '18 at 6:54






  • 1




    Yes, thanks! I will respond later
    – Jaba
    Nov 23 '18 at 7:01






  • 1




    Ok, now that’s more my liking, look at that simplicity
    – Jaba
    Nov 23 '18 at 7:19










  • @Jaba Happy to help, :-), 😊😊😊, (just got back to stackoverflow)
    – U9-Forward
    Nov 24 '18 at 23:39










  • @Jaba Haven't so a reply yet as promised :-), tho you can answer anytime, lol :D, 😜
    – U9-Forward
    Nov 28 '18 at 4:05


















  • @Jaba i edited mine, Btw, you received my email?
    – U9-Forward
    Nov 23 '18 at 6:54






  • 1




    Yes, thanks! I will respond later
    – Jaba
    Nov 23 '18 at 7:01






  • 1




    Ok, now that’s more my liking, look at that simplicity
    – Jaba
    Nov 23 '18 at 7:19










  • @Jaba Happy to help, :-), 😊😊😊, (just got back to stackoverflow)
    – U9-Forward
    Nov 24 '18 at 23:39










  • @Jaba Haven't so a reply yet as promised :-), tho you can answer anytime, lol :D, 😜
    – U9-Forward
    Nov 28 '18 at 4:05
















@Jaba i edited mine, Btw, you received my email?
– U9-Forward
Nov 23 '18 at 6:54




@Jaba i edited mine, Btw, you received my email?
– U9-Forward
Nov 23 '18 at 6:54




1




1




Yes, thanks! I will respond later
– Jaba
Nov 23 '18 at 7:01




Yes, thanks! I will respond later
– Jaba
Nov 23 '18 at 7:01




1




1




Ok, now that’s more my liking, look at that simplicity
– Jaba
Nov 23 '18 at 7:19




Ok, now that’s more my liking, look at that simplicity
– Jaba
Nov 23 '18 at 7:19












@Jaba Happy to help, :-), 😊😊😊, (just got back to stackoverflow)
– U9-Forward
Nov 24 '18 at 23:39




@Jaba Happy to help, :-), 😊😊😊, (just got back to stackoverflow)
– U9-Forward
Nov 24 '18 at 23:39












@Jaba Haven't so a reply yet as promised :-), tho you can answer anytime, lol :D, 😜
– U9-Forward
Nov 28 '18 at 4:05




@Jaba Haven't so a reply yet as promised :-), tho you can answer anytime, lol :D, 😜
– U9-Forward
Nov 28 '18 at 4:05













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.






share|improve this answer





















  • 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
    – Jaba
    Nov 23 '18 at 6:48
















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.






share|improve this answer





















  • 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
    – Jaba
    Nov 23 '18 at 6:48














1












1








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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 6:35









Martin Thoma

40.7k53292510




40.7k53292510












  • 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
    – Jaba
    Nov 23 '18 at 6:48


















  • 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
    – Jaba
    Nov 23 '18 at 6:48
















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
– Jaba
Nov 23 '18 at 6:48




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
– Jaba
Nov 23 '18 at 6:48


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441591%2fis-it-possible-to-return-a-value-while-still-modifying-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo