Python __index__ special method

前端 未结 3 1984
南旧
南旧 2021-01-01 19:47
>>> class Thing(object):
...     def __index__(self):
...         return 1
... 
>>> thing = Thing()
>>> list_ = [\'abc\', \'def\', \'ghi\'         


        
3条回答
  •  长情又很酷
    2021-01-01 20:39

    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):
        return 1
    
    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.

提交回复
热议问题