[issue15289] Adding __getitem__ as a class method doesn't work as expected
Eric Snow
report at bugs.python.org
Sat Jul 7 22:32:32 CEST 2012
Eric Snow <ericsnowcurrently at gmail.com> added the comment:
This appears to be a misunderstanding about special-method lookup which, honestly, isn't super obvious at first. It helps to remember that classes are objects like everything else in Python and thus are instances of some type (their metaclass).
Anyway, here's the key point regarding special-method lookup. As far as Python is concerned, the following are equivalent:
value = obj[key]
and
value = type(obj).__getitem__(obj, key)
Thus Alias()["f'"] will give you your answer, but Alias["f'"] tries to call type(Alias).__getitem__(Alias, key). Since Alias is a class, it is an instance of the built-in type class, so type(alias) is type, which does not have a __getitem__() method.
Check out the following resources:
http://docs.python.org/py3k/reference/datamodel.html#special-method-names
http://docs.python.org/py3k/library/inspect.html#fetching-attributes-statically
----------
nosy: +eric.snow
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15289>
_______________________________________
More information about the Python-bugs-list
mailing list