Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

LoveMathFermat

  • one year 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
    • one year 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
    • one year 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
    • one year 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

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.