I'm trying to add to an iterator, some custom properties like:
iterator = iter([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
iterator.__custom_property__ = 'random value'
But all I get is this error that says that the iterator attribute are read only:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'listiterator' object has no attribute '__custom_property__'
This question is linked to this other one: Get length of a (non infinite) iterator inside it's loop using Python 2.7.
What I thought is this: If I can somehow add some custom data into the iterator object that the itertools.imap
returns to me, I could access to those information inside any loop that loops over the iterator.
But, apparently, those data cannot be stored.
At least using this workflow.
Does anyone know how to "hack" into an iterator object (specifically a listiterator
) and define some custom attributes or methods?
Thanks!
INFO: I thought that, because of the argument of this question, could be worth asking in another thread instead of mixing the information in the other one.