
On 9 Oct 2023, at 08:58, Stephen J. Turnbull <turnbull.stephen.fw@u.tsukuba.ac.jp> wrote:
... Not sure what you're getting at here, that's an infloop. Did you mean something like this:
class A: def func(self): while (self.a := self.a += 1) < 5: pass return self.a Mistake, late night. I just meant to portray initialisation via walrus inside statement, nothing more. It doesn’t work with while loop. The intention was this, which is also principal use-case from previous reply: class A: def func(self, string): if self.a := re.match('.*', string): print(self.a) return self.a.group() Generally, this would be useful for all examples of https://peps.python.org/pep-0572/ <https://peps.python.org/pep-0572/> , where “initial” value is an attribute or dictionary item.
Same argument as for “walrus” operator itself - convenient feature. Or is there more to it?
There's more to it. The loop and a half construct, where you execute the body of the loop once outside the loop for some reason is widely considered a really big wart (DRY failure). The walrus allows us to eliminate most of those.
On the other hand, an assignment and a return statement are two different things; it's not a DRY viotion to have the same identifier in both. 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?
I was actually surprised that this didn’t work - I thought that this operator is a literal composite of assignment and “retriever", rather than having it’s own restrictive logic.
This is a common practice in Python development. Try a little bit of a new idea, avoid engineering, and expand later if that seems useful too. Fair.
If it doesn’t break anything, doesn’t have any undesirable side effects and community likes it, then could be a good addition.
"Community likes it" is a null predicate, very hard to verify in edge cases. When the support in the community is strong enough that "community likes it" is common knowledge, it's always the case that there are objective reasons why it's a good thing.
All additions have a 0 * infinity cost: a negligible cost of learning (for one user) times *all* the users. Ok, thanks.
DG