Giả sử lớp sau đây:tại sao __getitem__ không thể là lớp học?
class Class(object):
@classmethod
def getitem(*args):
print 'getitem %s' % (args,)
@classmethod
def __getitem__(*args):
print '__getitem__ %s' % (args,)
Phương pháp GetItem cư xử như mong đợi: nó nhận Class
như arg đầu tiên, nhưng __getitem__
nhận type
như arg đầu tiên:
calling Class.getitem(test)
getitem (<class '__main__.Class'>, 'test')
calling obj.getitem(test)
getitem (<class '__main__.Class'>, 'test')
calling Class[test]
'type' object has no attribute '__getitem__'
calling obj[test]
__getitem__ (<class '__main__.Class'>, 'test')
sự kỳ diệu là gì đó đằng sau __getitem__
?
'Suy nghĩ về Class là một thể hiện của loại'. Cảm ơn bạn. Cảm ơn bạn. Cảm ơn bạn. – norbertpy