Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

LoveMathFermat

  • 3 years ago

Hi fellow OCW students, I'm kind of lost on problem 2 of Problem set 9. I still don't get the idea of __iter__(self): Can you guys give me some explanation on what it is and what it does?

  • This Question is Closed
  1. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you are supposed to return an object that will 'yield' the next item when its next method is called. http://docs.python.org/2.7/reference/datamodel.html#special-method-names http://docs.python.org/2.7/reference/datamodel.html#object.__iter__ http://docs.python.org/2.7/library/stdtypes.html#iterator-types http://docs.python.org/2.7/tutorial/classes.html#iterators A google search of - python custom class iterator - returns a number of useful 'hits'

  2. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    A simple, but clear example from http://johnmc.co/llum/the-easiest-way-to-implement-__iter__-for-a-python-object/ Let's say you write your own class (and it stores a list of vertices and you want to use loop over all vertices using a for loop. How does the for loop know what the next value of your object would be? That's where the __iter__ method comes in. In the __iter__ method, you'll need to say what data is returned when you want to iterate over an object. They can be strings, numbers, anything you want.

  3. LoveMathFermat
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you guys!

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy