Python Forum
Is that possible to have a new feature: dynamic return with using function argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is that possible to have a new feature: dynamic return with using function argument
#1
In python 3.8, we will have a new syntax := that could assign values to variables as part of larger expression, for example,
if (n:=len(a)) > 10:
    ....
On the other hand, I also think that it would be useful if := could be used in function argument, so that a local variable created inside a function could be returned **optionally**, for example,
def func(arg1, arg2, karg1=karg1_default, karg_return:=karg_return):
    
    ...
    karg_return = 3
    ...
    
    return func_output

# normal calling of func() without returning variable karg_return
# in this way, karg_return:=karg_return is not used and karg_return is only simply a local variable in func()
output = func(2,3,karg1=4)

# calling func() with using the karg_return argument
# in this way, value of the local variable karg_return in func() is created and "passed" to variable a
output = func(2,3,karg1=4,a:=karg_return)
print(a) # a = 3
Is that possible to have this new feature in new python version?
Reply
#2
I donĀ“t know, as this is not our decision to make.
If you think this would be a nice feature in future python you can propose this
to Python developers https://www.python.org/dev/peps/
If your idea is good they will think about it.
Reply
#3
@ThomasL
Thanks a lot. I will post my proposal to PEPs.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  return in a function Skaperen 16 7,751 Mar-16-2021, 07:54 PM
Last Post: Skaperen
  function/method keyword argument alias Skaperen 1 3,098 Dec-01-2019, 03:56 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020