So, I created a custom class in Python that supports iteration using the __iter__ method. My __iter__ method __iter__ structured as a generator object, using yield to give each of the values in an instance of the class.
I find that if I say something like x = iter(my_instance) and then edit the data stored in my instance, if I subsequently create a list from x , I will find that this list contains the values that my_class currently has, not the values it had when iter() called.
So my question is this time doubled. First of all, is this what should happen? If so, why does it work?
Above is the main thing, but if you really want to go higher and further, read on. Now, the reason I am asking this question is because I needed to create this class as a task at the university. My professor provided a program that will do a bunch of tests in this class that I created to give me an idea of whether I did it right the first time.
My program passes every check except the last one, which basically does what is described above. The problem is that he expects the list constructed from the previous call to iter(my_object) to reflect only the values stored in my_object , while iter() was called on it.
My initial reaction after considering this was that my professor intended to get a result that looked like just calling list(my_object) , but I'm a student here, so most likely I just encoded my __iter__ method in this very weird way, which causes this behavior. Or has Python changed the way iter() functions recently?
I would prefer not to publish my code, as this is for the class, and this could technically be considered a hoax (I know this is stupid and useless, sorry). However, I can bind the specification, which is right here (in particular, part 1, class Bag ).