On Tue, May 25, 2021 at 5:29 PM Steven D'Aprano <email@example.com> wrote:
> Here's a counter-proposal: we have a special symbol which is transformed
> at compile-time to the left hand assignment target as a string. Let's
> say we make that special expression `@@` or the googly-eyes symbol.
This is sounding promising. I'm liking this.
> Chained assignments transform to a tuple of target names:
> spam = eggs = cheese = func(arg, @@)
> # spam = eggs = cheese = func(arg, ('spam', 'eggs', 'cheese'))
Hmm. Everything else gives you a single string, this one doesn't. I'd
actually be inclined to switch around this one and the next one...
> Sequence unpacking assignment gets transformed as a single
> comma-seperated string:
> spam.eggs, foo, *bar = func(arg, @@)
> # spam.eggs, foo, *bar = func(arg, ('spam.eggs,foo,*bar'))
... so that assigning the same thing to multiple names gives you a
space-separated string (or equals-separated, "spam=eggs=cheese"), but
unpacking gives you a tuple of targets, since it then nicely parallels
the result it's expecting from the function. That would mean that:
# This assigns a single string to them all eg "spam eggs cheese"
spam = eggs = cheese = @@
# This assigns a string to each one:
spam, eggs, cheese = @@
# and is equivalent to:
spam = @@; eggs = @@; cheese = @@
No wrong answers. (Well, unless you say "tomato". That is a very wrong
answer to a yes/no question.)
I'm liking this. It might mean that class syntax and decorator abuse
become less necessary as ways to get around name duplication.