Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:26:23+00:00 2026-05-31T02:26:23+00:00

I have a class in python, which has an iterable as instance variable. I

  • 0

I have a class in python, which has an iterable as instance variable. I want to iterate the instances of the class by iterating over the embedded iterable.

I implemented this as follows:

def __iter__(self):
    return self._iterable.__iter__()

I don’t really feel that comfortable calling the __iter__() method on the iterable, as it is a special method. Is this how you would solve this problem in python or is there a more elegant solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:26:24+00:00Added an answer on May 31, 2026 at 2:26 am

    The “best” way to way to delegate __iter__ would be:

    def __iter__(self):
        return iter(self._iterable)
    

    Alternately, it might be worth knowing about:

    def __iter__(self):
        for item in self._iterable:
            yield item
    

    Which will let you fiddle with each item before returning it (ex, if you wanted yield item * 2).

    And as @Lattyware mentions in the comments, PEP380 (slated for inclusion in Python 3.3) will allow:

    def __iter__(self):
        yield from self._iterable
    

    Note that it may be tempting to do something like:

    def __init__(self, iterable):
        self.__iter__ = iterable.__iter__
    

    But this won’t work: iter(foo) calls the __iter__ method on type(foo) directly, bypassing foo.__iter__. Consider, for example:

    class SurprisingIter(object):
        def __init__(self):
            self.__iter__ = lambda self: iter("abc")
    
        def __iter__(self):
            return iter([1, 2, 3])
    

    You would expect that list(SurprisingIter()) would return ["a", "b", "c"], but it actually returns [1, 2, 3].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python class I want to instantiate and the __init__ definition has
I have some python module, which has a class ModuleClass and I can't modify
I want to be able use python descriptors in a class which has the
I have a class named Individual, which has a variable, self.fitness. I have a
I have a python class which reads a config file using ConfigParser: Config file:
You have a Python class which needs an equals test. Python should use duck-typing
I have a small Python OOP program in which 2 class, Flan and Outil
Suppose I have a Python class that I want to add an extra property
I have a C++ class, which has the following methods: class Bar { ...
I have a model class Place, which has a field for state, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.