[Python-ideas] Allowing def to assign to anything

Chris Angelico rosuav at gmail.com
Mon Oct 26 22:48:36 EDT 2015


On Mon, Oct 26, 2015 at 6:05 PM, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
> On Oct 25, 2015, at 23:23, Alexander Walters <tritium-list at sdamon.com> wrote:
>>
>> I imagine in the case of assigning to a class or instance, the name would be the same (and binding to self would act the same) as if it were assigned in the traditional way.
>
> In what traditional way? When you def a function, its name is the name given in the def statement. If you later assign it to a member of an object, that doesn't change its name. So, that doesn't answer the question.
>

dispatch = {}
def dispatch['foo'](): pass
assert dispatch['foo'].__name__ == "dispatch['foo']"

I have no problem with this, conceptually. Syntactically, it would be
defined as f"dispatch[{key!r}]" if the key is allowed to be an
arbitrary expression.

ChrisA


More information about the Python-ideas mailing list