[Python-ideas] PEP 572: Assignment Expressions (post #4)

Thautwarm Zhao yaoxiansamma at gmail.com
Thu Apr 12 23:35:11 EDT 2018


>  None if var:= function() is None else var.method()

Make sense.

In some specific scenes(maybe general, I'm not sure),

   var.method() if var:=function() else var

looks cool although it's not really null checking. It works for Python,
just like use `if lst` to check if the list `lst` is empty.


2018-04-13 11:22 GMT+08:00 David Mertz <mertz at gnosis.cx>:

> Yes, I should have added ternary expressions to if statements. I can
> definitely see the use there.
>
> However, your example is not null checking. You'd have to modify it
> slightly to get that:
>
> None if var:= function() is None else var.method()
>
> Still not bad looking.
>
> On Thu, Apr 12, 2018, 11:01 PM Thautwarm Zhao <yaoxiansamma at gmail.com>
> wrote:
>
>>
>> > You're looking at a very early commit there. I suggest looking at the
>> > most recent commits on one of two branches:
>>
>> https://github.com/Rosuav/cpython/blob/statement-local-
>> variables/Grammar/Grammar
>> https://github.com/Rosuav/cpython/blob/assignment-
>> expressions/Grammar/Grammar
>>
>> > Those are the two most recent states in my progress towards (a)
>> > statement-local name bindings with "EXPR as NAME", and (b) assignment
>> > expressions with "target := value".
>>
>> Okay, and the syntax "target := value" seems to be much easier to handle
>> with.
>>
>> I do support this feature, but now I'm worried about the meaning of ':=',
>> it seems to be a lazy assignment in some degree. I'm not sure using ':=' is
>> proper here.
>>
>>
>> > Inasmuch as I might like assignment expressions, it would only be in
>> while or if statements, personally.
>>
>> Not exactly, assignment expression also works for "if expression", we can
>> now have null checking.
>>
>>     var.method() if var:= function() else None
>>
>> Null checking is importance enough to have a specific syntax in many
>> other languages(C#, kotlin, Ruby and so on), and we can even have more than
>> null checking by adding expression assignment.
>>
>>
>>
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180413/fb5c1c1e/attachment.html>


More information about the Python-ideas mailing list