New assignmens ...
Chris Angelico
rosuav at gmail.com
Sun Oct 24 11:18:27 EDT 2021
On Mon, Oct 25, 2021 at 2:13 AM Alan Bawden <alan at csail.mit.edu> wrote:
>
> It seemed weird to me that only an identifier was allowed to be the
> target of an assignment expression. Then it occurred to me that
> function definitions are another place where only identifiers are
> allowed, but where I could imagine an attributeref or a subscription
> being used. E.g.
>
> def table[i](x):
> ...
>
> would mean the same thing as:
>
> def _temp_(x):
> ...
> table[i] = _temp_
>
> I don't immediately see that this would screw up the grammar in any way,
> so why not allow it? A `def` statement is just another form of
> assignment, so just as for `:=` expressions, we should allow the target
> to be as general as possible!
>
> I'm guessing that about 70% of you will think that this is a horrible
> idea, 10% of you will find it compelling, and the remaining 20% will
> find themselves conflicted. You can count me in that last category...
This has come up periodically, but it's a bit tricky to define some of
the edge cases, like what the function's name should be. But it would
definitely help with building dispatch tables. Currently, I tend to
build them with a decorator:
tools = {}
def tool(f):
tools[f.__name__] = f
return f
@tool
def frobnicate(): ...
@tool
def spamify(): ...
In theory, it would be possible to do this:
def tools["frobnicate"](): ...
But I'm not sure that it's truly better.
ChrisA
More information about the Python-list
mailing list