On Sunday, 8 June 2014 15:59:14 UTC+1, Roy Smith wrote:
> Why? I assume a language which promoted the global namespace to be in
> the attribute search path (which, as far as I can tell, is what we're
> talking about here) would implement hasattr and raising AttributeError
> in a consistent way.
It's slightly different. Although I used len() as an example, the idea is to
allow any function to be used in this way, including local symbols.
e.g. I could define:
return x * x
i = 3
i.squared() => 9
j = AClassThatImplements__mul__()
j.squared() => whatever j * j returns
def __mul__(self, other):
k = AnotherClass()
k.squared() => calls method, not function
In this case, there is a problem with letting hasattr('squared') return True
for these first two instances. See Ian's post for a description of the problem.