[Python-ideas] Re: SyntaxError: cannot use assignment expressions with attribute

2023-10-09 Thread Dom Grigonis
I just want to note that this is just a gentle suggestion/observation, in case 
this wasn’t done yet.

I am not pushing this.

——

All good points, thank you.

——

Why would this not be a good option? 1 extra line compared to walrus, but no 
DRY issue.
with open(“fn") as f:
while True:
line = f.readline()
if line and check(line):
process(line)
else:
break
DG

> On 9 Oct 2023, at 15:52, Stephen J. Turnbull 
>  wrote:
> 
> Dom Grigonis writes:
> 
>> Mistake, late night. I just meant to portray initialisation via
>> walrus inside statement, nothing more. It doesn’t work with while
>> loop.
> 
> It *does* work with a while loop, just not that one.  See below.  The
> problem is that you're writing new code full of trash you don't need
> to explain the concept.  Keep these examples as simple as possible.
> For example, my "pass" example is too simple if you want to show how
> the walrus can save typing because it doesn't save any lines of code;
> you need something a little more complicated to demonstrate that
> saving.
> 
> In the progress of any proposal to acceptance, at some point someone
> is going to say, "I see that it works and solves a theoretical
> problem, but who would ever use it?"  At that point you go into a
> large body of code such as the Python standard library or numpy to
> find realistic examples.  But that's old code, and only needed to
> demonstrate actual utility.
> 
>> Generally, this would be useful for all examples of
>> https://peps.python.org/pep-0572/ 
>> , where “initial” value is an attribute or dictionary item.
> 
> That's not the question though.  The question is are those examples
> themselves frequently useful.  And that's when you go find them in the
> stdlib.
> 
>> I see. Could you give an example of this? I vaguely remember this
>> sometimes being the case, but can not find anything now on this in
>> relation to walrus operator. Is there no way to bring that
>> execution within the body without walrus operator?
> 
> The problem is code like this:
> 
>with open("a_file") as f:
>while (line := f.readline())
>if check(line):
>process(line)
>else:
>break
> 
> There is no way to do that without an assignment, and without the
> walrus you need an assignment before the while, and the same
> assignment within the loop body.  Is that a big deal?  No.  In fact, I
> have never used the walrus operator in anger, nor wanted to.  I'm just
> as happy to write
> 
>with open("a_file") as f:
>line = f.readline()
>while (line)
>if check(line):
>process(line)
>else:
>break
>line = f.readline()
> 
> But some people find loop-and-a-half extremely ugly, and while I
> differ on "extremely", I can't deny that it's ugly.
> 
> You *could* do this for small files with
> 
>with open("a_file") as f:
>lines = f.readlines()
>for (line in lines):
>if check(line):
>process(line)
>else:
>break
> 
> but that's not possible with an infinite iterable, undesireable for
> most non-file streams, etc.
> 
> Re "simple examples", see why I used the "if check()" stuff?  If I
> wasn't going to talk about infinite iterables and pausing external
> streams, that would just be (potentially wrong!) complexity that
> doesn't help explain anything.
> 
> Steve
> 
> 

___
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/B2TLFMOOYMRRXCRJLLFV6F5URU4OPO3J/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: SyntaxError: cannot use assignment expressions with attribute

2023-10-09 Thread Stephen J. Turnbull
Dom Grigonis writes:

 > Mistake, late night. I just meant to portray initialisation via
 > walrus inside statement, nothing more. It doesn’t work with while
 > loop.

It *does* work with a while loop, just not that one.  See below.  The
problem is that you're writing new code full of trash you don't need
to explain the concept.  Keep these examples as simple as possible.
For example, my "pass" example is too simple if you want to show how
the walrus can save typing because it doesn't save any lines of code;
you need something a little more complicated to demonstrate that
saving.

In the progress of any proposal to acceptance, at some point someone
is going to say, "I see that it works and solves a theoretical
problem, but who would ever use it?"  At that point you go into a
large body of code such as the Python standard library or numpy to
find realistic examples.  But that's old code, and only needed to
demonstrate actual utility.

 > Generally, this would be useful for all examples of
 > https://peps.python.org/pep-0572/ 
 > , where “initial” value is an attribute or dictionary item.

That's not the question though.  The question is are those examples
themselves frequently useful.  And that's when you go find them in the
stdlib.

 > I see. Could you give an example of this? I vaguely remember this
 > sometimes being the case, but can not find anything now on this in
 > relation to walrus operator. Is there no way to bring that
 > execution within the body without walrus operator?

The problem is code like this:

with open("a_file") as f:
while (line := f.readline())
if check(line):
process(line)
else:
break

There is no way to do that without an assignment, and without the
walrus you need an assignment before the while, and the same
assignment within the loop body.  Is that a big deal?  No.  In fact, I
have never used the walrus operator in anger, nor wanted to.  I'm just
as happy to write

with open("a_file") as f:
line = f.readline()
while (line)
if check(line):
process(line)
else:
break
line = f.readline()

But some people find loop-and-a-half extremely ugly, and while I
differ on "extremely", I can't deny that it's ugly.

You *could* do this for small files with

with open("a_file") as f:
lines = f.readlines()
for (line in lines):
if check(line):
process(line)
else:
break

but that's not possible with an infinite iterable, undesireable for
most non-file streams, etc.

Re "simple examples", see why I used the "if check()" stuff?  If I
wasn't going to talk about infinite iterables and pausing external
streams, that would just be (potentially wrong!) complexity that
doesn't help explain anything.

Steve


___
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/3SSXFFCSGLMJ4HBDJXOP6KFJDCGEXZDE/
Code of Conduct: http://python.org/psf/codeofconduct/