list - Issue overriding __get__ in Python -
i have class:
from collections import userlist class itemlist(userlist): data = [] def __init__(self, contents): self.data = contents def __get__(self, index): result = list.__get__(self, index) if type(result) list: if len(result) > 1: return itemlist(result) else: return result
it seems in case get isn't being called when index instance of itemlist
class. i'm trying return new instance of itemclass
if result of index returns more 1 item (a list). expect this:
>>> il = itemlist(contents) >>> type(il[1:3]) <class 'itemlist'>
but i'm getting this:
>>> il = itemlist(contents) >>> type(il[1:3]) <class 'list'>
what doing wrong?
i think want more following:
class itemlist(userlist): data = [] def __init__(self, contents): super().__init__() self.data = contents def __getitem__(self, item): result = userlist.__getitem__(self, item) if type(result) list: if len(result) > 1: return itemlist(result) else: return result
Comments
Post a Comment