I want to pass a parameter to control the size of yielded data. For example,
class Something:
def __init__(self):
self.all_data = list(range(23))
def __iter__(self, size):
random.shuffle(self.all_data)
batch = list()
for i in self.all_data:
batch.append(i)
if len(batch) >= size:
yield batch
batch = list()
a = Something()
for i in a: # TypeError: __iter__() missing 1 required positional argument: 'size'
print(i)
for i in a(5): # TypeError: 'Something' object is not callable
print(i)
__iter__must return an iterator, whatever you're doing should go in__next__(without any additional parameter, take parameters when doing initialization and use those attributes).sizeparameter during initialization. But this means that an instance can only set onesizeparameter. The yielded data size cannot be dynamically adjusted.