[Python-ideas] Re: [new idea] dynamic return with using function argument

2019-08-26 Thread HUANG YUWEI
> In 3.8, foo(a := 42) is in fact valid syntax, meaning the same as foo((a
> := 42)).
>

Yes, I ignored that assign operator is actually a  valid syntax in function
calling, and what I need is new syntax.
Although the "dynamic return" could be implemented, I agree that it is
unworthy and namedtuple or a class could solve the problem.

Thanks for all your kind explanation. Let us close this conversation.

Huang Y.W

Greg Ewing  于2019年8月27日周二 上午8:01写道:

> HUANG YUWEI wrote:
> > the number of return variables could be many. Then it would look like
> > ```
> > res0, res1, res2, res3, res4, res5, res6, res7, ..., resN =
> > high_level_function()
>
> At that point you're better off returning an object with named
> attributes, such as a namedtuple or a class designed for the
> purpose.
>
> --
> Greg
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/2JOWUVLAH5SKE3ZOIAVA6R7SG2RKJQH2/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KGWVRXGL2YI2RSZ34BNITCVXUOKI7JTF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: [new idea] dynamic return with using function argument

2019-08-26 Thread HUANG YUWEI
Dear Mr. Guido,

Ok, I see.
Thank you for your prompt reply.

Best regards,
Huang Y.W

Guido van Rossum  于2019年8月27日周二 上午12:38写道:

> Sorry, there is no way to leverage the implementation or syntax of := for
> this purpose. You must be misunderstanding how they it works.
>
> You are right that the initial value of the variable at the call site is
> irrelevant (but there must be some initial value, e.g. None, to be able to
> identify the variable).
>
> Surely there are other ways to deal with the UX problem you indicate (e.g.
> namedtuple).
>
> --Guido
>
> On Mon, Aug 26, 2019 at 8:33 AM HUANG YUWEI  wrote:
>
>> Dear Mr. Guido and Mr. Spealman,
>>
>> Thanks for your quick reply.
>>
>> Yes, something like "Out Parameters in C#" is exactly what I mentioned.
>>
>> "calling by reference" is very close, except that
>> - with "calling by reference", the object is initialized before the
>> function is called, and then being passed to function and modified.
>> - in my idea, the object is initialized (created) inside a function, and
>> its pointer (or reference) is passed to a variable X as the initialization
>> of variable X.
>> The point is that, the returned value may not have fixed type or array
>> shape due to the possible existence of "if condition" inside the function.
>> On the other hand, "calling by reference" fixes these attributes of
>> returned variable.
>>
>> And also you are right, this could be easily solved by returning a tuple
>> of multiple values, and the users just pick the returned value they need.
>> For low level function, the number of return variables may be 1 or 2; For
>> high level function which comprises of multiple low level functions, the
>> number of return variables could be many. Then it would look like
>> ```
>> res0, res1, res2, res3, res4, res5, res6, res7, ..., resN =
>> high_level_function()
>> ```
>> or
>> ```
>> results = high_level_function()
>> res0 = results[0]
>> res1 = results[1]
>> ...
>> ```
>> This hurts the readability of code sometimes.
>>
>> My idea is simply that, okay we are now able to have assign operator in
>> large expression, then why not in a function calling statement. (if the
>> implementation is not difficult ...)
>>
>> best regards,
>> Huang Y.W
>>
>> Calvin Spealman  于2019年8月27日周二 上午12:00写道:
>>
>>> This also looks a lot like Out Parameters in C# (
>>> https://www.c-sharpcorner.com/article/out-parameter-in-c-sharp-7/)
>>>
>>> They have a similar dichotomy of declaration and call time semantics
>>> using the `out` keyword rather than an assignment operator.
>>>
>>> On Mon, Aug 26, 2019 at 10:50 AM Guido van Rossum 
>>> wrote:
>>>
>>>> I think you're talking about call-by-reference (Google it).
>>>>
>>>> What would be your use case?
>>>>
>>>> Do you know you can return multiple values from a function using a
>>>> tuple? E.g.
>>>>
>>>> def foo():
>>>> return 3, 42
>>>>
>>>> x, y = foo()
>>>> print(x)  # 3
>>>> print(y)  # 42
>>>>
>>>> --Guido
>>>>
>>>> On Mon, Aug 26, 2019 at 5:57 AM HUANG YUWEI 
>>>> wrote:
>>>>
>>>>> Dear Python community,
>>>>>
>>>>> I am a heavy python user in numerical simulation.
>>>>>
>>>>> In python 3.8, we will have a new syntax `:=` that could assign values
>>>>> to variables as part of larger expression like,
>>>>>
>>>>> ```
>>>>>
>>>>> 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_retur

[Python-ideas] Re: [new idea] dynamic return with using function argument

2019-08-26 Thread HUANG YUWEI
Dear Mr. Guido and Mr. Spealman,

Thanks for your quick reply.

Yes, something like "Out Parameters in C#" is exactly what I mentioned.

"calling by reference" is very close, except that
- with "calling by reference", the object is initialized before the
function is called, and then being passed to function and modified.
- in my idea, the object is initialized (created) inside a function, and
its pointer (or reference) is passed to a variable X as the initialization
of variable X.
The point is that, the returned value may not have fixed type or array
shape due to the possible existence of "if condition" inside the function.
On the other hand, "calling by reference" fixes these attributes of
returned variable.

And also you are right, this could be easily solved by returning a tuple of
multiple values, and the users just pick the returned value they need.
For low level function, the number of return variables may be 1 or 2; For
high level function which comprises of multiple low level functions, the
number of return variables could be many. Then it would look like
```
res0, res1, res2, res3, res4, res5, res6, res7, ..., resN =
high_level_function()
```
or
```
results = high_level_function()
res0 = results[0]
res1 = results[1]
...
```
This hurts the readability of code sometimes.

My idea is simply that, okay we are now able to have assign operator in
large expression, then why not in a function calling statement. (if the
implementation is not difficult ...)

best regards,
Huang Y.W

Calvin Spealman  于2019年8月27日周二 上午12:00写道:

> This also looks a lot like Out Parameters in C# (
> https://www.c-sharpcorner.com/article/out-parameter-in-c-sharp-7/)
>
> They have a similar dichotomy of declaration and call time semantics using
> the `out` keyword rather than an assignment operator.
>
> On Mon, Aug 26, 2019 at 10:50 AM Guido van Rossum 
> wrote:
>
>> I think you're talking about call-by-reference (Google it).
>>
>> What would be your use case?
>>
>> Do you know you can return multiple values from a function using a tuple?
>> E.g.
>>
>> def foo():
>> return 3, 42
>>
>> x, y = foo()
>> print(x)  # 3
>> print(y)  # 42
>>
>> --Guido
>>
>> On Mon, Aug 26, 2019 at 5:57 AM HUANG YUWEI 
>> wrote:
>>
>>> Dear Python community,
>>>
>>> I am a heavy python user in numerical simulation.
>>>
>>> In python 3.8, we will have a new syntax `:=` that could assign values
>>> to variables as part of larger expression like,
>>>
>>> ```
>>>
>>> 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 it possible to add this new feature?
>>>
>>>
>>> Thanks for your attention to this matter.
>>>
>>> Huang Y.W.
>>> ___
>>> Python-ideas mailing list -- python-ideas@python.org
>>> To unsubscribe send an email to python-ideas-le...@python.org
>>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>>> Message archived at
>>> https://mail.python.org/archives/list/python-ideas@python.org/message/ZN5XM5MSHAAN4U3VBAEBXUJB4ZUN6SUP/
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>> *Pronouns: he/him/his **(why is my pronoun here?)*
>> <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world

[Python-ideas] [new idea] dynamic return with using function argument

2019-08-26 Thread HUANG YUWEI
Dear Python community,

I am a heavy python user in numerical simulation.

In python 3.8, we will have a new syntax `:=` that could assign values to
variables as part of larger expression like,

```

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 it possible to add this new feature?


Thanks for your attention to this matter.

Huang Y.W.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZN5XM5MSHAAN4U3VBAEBXUJB4ZUN6SUP/
Code of Conduct: http://python.org/psf/codeofconduct/