Skip to content Skip to sidebar Skip to footer

Python __index__ Special Method

>>> class Thing(object): ... def __index__(self): ... return 1 ... >>> thing = Thing() >>> list_ = ['abc', 'def', 'ghi'] >>> list_[

Solution 1:

Dict and List does not implement __getitem__ the same way. Dict objects uses a comparison (__eq__) on __hash__ of objects as key to use in __getitem__.

To make Thing usable for dict you have to implement both hash and eq.

Solution 2:

Another example to understand it further, here _MolsToGridSVG takes a list argument. I wanted to limit the list to some length. Now here for python list, slice indices have to be used. The following implementation solved it. Basically here index is getting used for Python List.

def__index__(self):
    return1

imagesInOneFile = int(len(smilesMolList) / noOfFiles)
svg = Draw._MolsToGridSVG(smilesMolList[:imagesInOneFile.__index__()], subImgSize=(400, 200), molsPerRow=2)

Also one needs to remember that imagesInOneFile has to be integer.

Post a Comment for "Python __index__ Special Method"