[Python-ideas] Fwd: Define a method or function attribute outside of a class with the dot operator
Steven D'Aprano
steve at pearwood.info
Fri Feb 10 10:25:07 EST 2017
On Sat, Feb 11, 2017 at 01:25:40AM +1100, Chris Angelico wrote:
> For what it's worth, my answers would be:
>
> __name__ would be the textual representation of exactly what you typed
> between "def" and the open parenthesis. __qualname__ would be built
> the exact same way it currently is, based on that __name__.
If I'm reading this right, you want this behaviour:
class Spam:
pass
def Spam.func(self): pass
assert 'Spam.func' not in Spam.__dict__
assert 'func' in Spam.__dict__
assert Spam.func.__name__ == 'Spam.func'
assert Spam.func.__qualname__ == 'Spam.Spam.func'
If that's the case, I can only ask... what advantage do you see from
this? Because I can see plenty of opportunity for confusion, and no
advantage.
For what its worth, Lua already has this feature:
http://www.lua.org/pil/6.2.html
Lib = {}
function Lib.foo (x,y)
return x + y
end
If we define that function foo inside the Lib table, and then cause an
error, the Lua interpreter tells us the function name:
> Lib.foo('a', 1)
stdin:2: attempt to perform arithmetic on local 'x' (a string value)
stack traceback:
stdin:2: in function 'foo'
stdin:1: in main chunk
[C]: in ?
--
Steve
More information about the Python-ideas
mailing list