python - How can I replace a class @property's len(..) return with a custom __len__() procedure? -
when class
defined (ex. class myclass
), possible replace returns len(my_class_instance)
using def __len__(self)
.
how can apply custom
class
@property
in below scenario?class carwash(object): def __init__(self): self._queue = [1, 2, 3] @property def queue(self): return self._queue
when len(car_wash.queue)
called, don't want list's len returned replace custom __len__
procedure, since want query db first.
how can this?
if don't want list.__len__
called, don't return list:
def carqueue(list): def __len__(self): return something_dubious() # if you're going override len, make sure overide `__iter__` # , `__getitem__` class carwash(object): def __init__(self): self._queue = carqueue([1, 2, 3]) @property def queue(self): return self._queue
Comments
Post a Comment