[Python-ideas] Continuation of `__name__` or a builtin function for general name getting
Alireza Rafiei
alireza.rafiei94 at gmail.com
Sun Jun 18 20:07:16 EDT 2017
Thanks for the explanation!
On Sun, Jun 18, 2017 at 4:00 PM, Chris Angelico <rosuav at gmail.com> wrote:
> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170618/1cb5b13f/attachment-0001.html>
More information about the Python-ideas
mailing list