The __name__ of a function has nothing to do with an Assign node, > which is simply assigning a value to something. For instance, if you > do: > >>> f = "hello" > you wouldn't expect the string "hello" to have a __name__ - it's just > a string. And a lambda function normally won't be assigned to > anything. You use lambda when there isn't any name: > >>> do_stuff(lambda q: q * 2 + 1) > and you use def when you want to assign it to a name: > >>> def f(): return True > By the time the Assign operation gets performed, the function object - > with all of its attributes, including __name__ - has been completely > created. I'm not sure what your proposal would do to these kinds of > situations, but it shouldn't be modifying the assigned object.
I guess I should have framed it as a `quote` for python. You're absolutely right that it shouldn't be modifying the assigned object and it doesn't. I mentioned the Assign to say that in `x = f`, `x` has name as well, however `x.__name__` returns the name of `f` and not `x`. As for the `f = "hello"`, the value of the name "f" would be "hello" and the value of the name "hello" would be "hello". My proposal is to either change the behavior of `__name__` or have something similar that acts globally for all objects and types to get a quote-like behavior, provided that the operands of quotes are atomic. On Sun, Jun 18, 2017 at 2:46 PM, Chris Angelico <[email protected]> wrote: > On Mon, Jun 19, 2017 at 7:38 AM, Alireza Rafiei > <[email protected]> wrote: > > I'm not sure whether this idea has been discussed before or not, so I > > apologize in advanced if that's the case. > > > > Consider the behavior: > > > >> >>> f = lambda: True > >> >>> f.__name__ > >> '<lambda>' > >> >>> x = f > >> >>> x.__name__ > >> '<lambda>' > > > > > > I'm arguing the behavior above is too brittle/limited and, considering > that > > the name of the attribute is `__name__`, not entirely consistent with > > Python's AST. Consider: > > > >> >>> f = lambda: True > >> >>> x = f > > > > > > At the first line, an ast.Assign would be created whose target is an > > ast.Name whose `id` is `f`. > > At the second line, an ast.Assign would be created whose target is an > > ast.Name whose `id` is `x`. > > The __name__ of a function has nothing to do with an Assign node, > which is simply assigning a value to something. For instance, if you > do: > > >>> f = "hello" > > you wouldn't expect the string "hello" to have a __name__ - it's just > a string. And a lambda function normally won't be assigned to > anything. You use lambda when there isn't any name: > > >>> do_stuff(lambda q: q * 2 + 1) > > and you use def when you want to assign it to a name: > > >>> def f(): return True > > By the time the Assign operation gets performed, the function object - > with all of its attributes, including __name__ - has been completely > created. I'm not sure what your proposal would do to these kinds of > situations, but it shouldn't be modifying the assigned object. > > ChrisA > _______________________________________________ > Python-ideas mailing list > [email protected] > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list [email protected] https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
