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

  • Home
  • SEARCH
  • 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 8065981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:50:25+00:00 2026-06-05T11:50:25+00:00

I am learning OOP python using the Markana tutorial and I’m having some trouble

  • 0

I am learning OOP python using the Markana tutorial and I’m having some trouble with Lab 16.8. (Among some other troubles) I can’t get the __iter__ method correct. My method looks like this:

def __iter__(self):
        for e in self.l_investments:
            yield e

Here is my code (and the result of running it): http://hastebin.com/wayuwakode.py

Here is the result that should appear:

>>> 'GOOG' in p
True

I get:

>>> 'GOOG' in p
False

p is an instance of the class Portfolio(). ‘GOOG’ is the .name property of an instance of the Investment() object. p contains a list and a dictionary of Investment() instances.

I can change my __iter__ method to yield the names:

def __iter__(self):
    for e in self.l_investments:
        yield e.name

This fixes it so that 'GOOG' in p == True, but it breaks another requirement:

Failed example:
    for stock in p:
        print stock
Expected:
    1000 shares of APPL worth 252730.00
    5000 shares of CSCO worth 118700.00
    500 shares of GOOG worth 245670.00
    2000 shares of MSFT worth 50880.00
Got:
    APPL
    CSCO
    GOOG
    MSFT

How do I change the __iter__ method (or any other part of the code) so that both of these requirements are met?

I more interested in learning how OOP works than solving this particular problem. Any help would be much appreciated!

  • 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-06-05T11:50:27+00:00Added an answer on June 5, 2026 at 11:50 am

    To add more detail to BrenBarn’s answer — yes, you want to override __contains__. The x in object membership test only uses __iter__ if __contains__ isn’t defined.

    It seems like you’ll want to make __contains__ work for both Investment instances and Investment name strings, since I’d assume you want both 'GOOG' in p and investment_object_with_GOOG_name in p to return True. If that’s the case, the code should look something like this:

    def ___contains___(self, item):
        # checks investment name strings by comparing to dictionary keys
        if item in self.d_investments:
            return True
        # checks investment objects by comparing to dictionary values
        if item in self.d_investments.values():
            return True
    

    Note that the Investment objects need to implement sensible comparison for this to work. If they don’t (which it looks like they don’t — object comparison uses IDs and not attribute contents), either give them a comparison operator, or replace the second half of the above with this version to just do the comparison on the name:

        # checks investment objects by comparing to dictionary keys
        if isinstance(item, Investment) and item.name in self.d_investments:
            return True
    

    Of course, if you want the membership test to only work for name strings, just remove the second half altogether.

    Also, after looking at your code — is there any reason why you keep a list and a dictionary of investments in Portfolio? Either should be sufficient as an internal representation. I used the dictionary for my code above, but it would be easy to re-implement with a list — I think you should just pick one, unless you really need both for performance or other reasons that I haven’t figured out from the code.

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

Sidebar

Related Questions

I am learning OOP with python. I want to implement something like to display:
I am quite a newbie in OOP using java and I'm learning as I
I'm learning more about javascript OOP by rolling my own console.log variant with some
I have been recently learning some OOP PHP and seem to have run into
I grew up learning to set up data structures using OOP. But now as
I am learning OOP with python .trying with small console application Stock class Stock(object):
I'm learning php oop, going through books etc and having a go at my
I started learning OOP applied to C++. Now I want to know if I
Learning some VBA. So far, I've constructed this piece of code which should allow
learning the basics of Python, now ready for a bigger challange... it didn't take

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.