[Python-ideas] Continuation of `__name__` or a builtin function for general name getting

Chris Angelico rosuav at gmail.com
Sun Jun 18 19:00:21 EDT 2017


On Mon, Jun 19, 2017 at 8:47 AM, Alireza Rafiei
<alireza.rafiei94 at gmail.com> wrote:
>>  Hmm. So... after x = f,  f.__name__ would be different from x.__name__?
>
>
> Yes.

There's a couple of major problems with that. The first is that, in
Python, there is *absolutely no difference* between accessing an
object via one form and via another. For example:

>>> x = object() # or anything else
>>> y = [x, x, x]
>>> z = x
>>> q = {"spam": x}
>>> probably = globals()["x"]

You can access y[0], z, q["spam"], and (most likely) probably, and
they're all the same thing. Exactly the same object. So there's no way
to do attribute access on that object and get different results.

The second problem is that the current behaviour is extremely
important. One such place is with function decorators, which
frequently need to know the name of the function being worked on:

def command(func):
    parser.add_parser(func.__name__)
    ...

@command
def spaminate():
    ...

Inside the decorator, "func.__name__" has to be the name of the
function being decorated ("spaminate"), *not* "func". The function has
an identity and a canonical name. Disrupting that would cause major
difficulties for these kinds of decorators.

ChrisA


More information about the Python-ideas mailing list