मान लीजिए निम्नलिखित वर्ग:क्यों __getitem__ क्लासमेड नहीं हो सकता है?
class Class(object):
@classmethod
def getitem(*args):
print 'getitem %s' % (args,)
@classmethod
def __getitem__(*args):
print '__getitem__ %s' % (args,)
GetItem विधि बर्ताव करता है के रूप में उम्मीद: यह पहली बार आर्ग के रूप में Class
प्राप्त करता है, लेकिन __getitem__
पहले आर्ग के रूप में प्राप्त करता है type
:
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')
क्या __getitem__
के पीछे वहाँ जादू है?
'कक्षा के प्रकार के उदाहरण के रूप में सोच रहा है'। धन्यवाद। धन्यवाद। धन्यवाद। – norbertpy