[Python-ideas] "Assignment expression" with function call-alike syntax

Kirill Balunov kirillbalunov at gmail.com
Wed May 23 14:48:07 EDT 2018

2018-05-23 17:54 GMT+03:00 Mike Miller <python-ideas at mgmiller.net>:

> On 2018-05-22 14:32, Kirill Balunov wrote:
>>     # in global scope everything works ok since locals is globals
>>      >>> while len( this( val = dummy() ) ) >= 0:
>>     ...     print(val)
>>     [0, 1]
>>     [0, 1, 2]
>>     [0, 1, 2, 3]
> Interesting!  Although the example with a len() and mutable default
> arguments obscured the utility, I thought.  Also, why limit it to one
> assignment?
I just want some dummy example which can not be trivially handled with `for
name in iter(func, value)`. But the latter is also only partly true. You
can make something like:

class P:
    __slots__ = ('func')
    def __init__(self, func):
        self.func = func
    def __eq__(self, other):
        return not self.func(other)

for val in iter(dummy, P(lambda x: len(x) < 5)):

[0, 1]
[0, 1, 2]
[0, 1, 2, 3]

The only restriction is that you can not pass arguments to a `func`, but
this, to some extent, can also be handled with lambda. I limit it to _one
assignment_ firstly to make it simple, and secondly to get rid of some edge

> Liked Terry's suggestions for name.  The name that jumped into my brain as
> I read was a reuse of the locals() or globals() callable with key words,
> that would change their behavior to what you suggest.

I like `this` because it is easy to follow in my opinion: " While this name
assigned to an expression is  greater than zero do... " But of course I'm
open to any other spelling. I'm just wondering, if someone like this form
as an alternative for assignment expression (`:=`).

With kind regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180523/0c227755/attachment.html>

More information about the Python-ideas mailing list