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 8933523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:33:36+00:00 2026-06-15T09:33:36+00:00

How can you extend in keyword to a class I made? I am making

  • 0

How can you extend “in” keyword to a class I made? I am making a card game with a Card class. There is another class which is a Hand of a player. Basically I want to see if a certain card is in a hand. An analogy is below:

>>> 5 in range(0, 5)
True

This is my code. I have a Hand class and I want to see if a Card() is in a Hand()

Also, I’m new to this concept of classes. I’m just starting to understand how this whole thing works. Did I implement len method correctly?

class Card:
    def __init__(self, suit, rank):
        # self.suit > str
        # self.rank > str
        if (suit in SUITS) and (rank in RANKS):
            self.suit = suit
            self.rank = rank
        else:
            self.suit = None
            self.rank = None
            print "Invalid card:", suit, rank

    def __str__(self):
        return self.suit + self.rank

    def get_suit(self):
        return self.suit

    def get_rank(self):
        return self.rank


# define hand class
class Hand:
    # A list of Card objects
    # adding cards to the hand should remove cards from the deck.

    def __init__(self):
        self.hand = []

    def __str__(self):
        cards = []
        for card in self.hand:
            cards += [card.get_suit() + card.get_rank()]
        return str(cards)

    def add_card(self, card):
        return self.hand.append(card)

    def __len__(self):
        counter = 0
        for card in self.hand:
            counter +=1
        return counter

OK, so I added this code in the hand class:

    def __contains__(self, card):
        return card in self.hand

but I tried testing my code and it doesn’t work:

c = Card('H','A')

h = Hand()
h.add_card(Card('S','K'))
h.add_card(Card('D','A'))
h.add_card(Card('H','A'))

print 'hand=', h
print 'c=', c
print 'c in h', c in h

It says False in terminal… Why??

  • 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-15T09:33:37+00:00Added an answer on June 15, 2026 at 9:33 am

    @BrenBarn gave you a pointer in the right direction to look at __contains__. However, as I commented on his answer, implementing that method will probably require that your Card objects be comparable. Right now, two cards will only appear equal if they are both the same object.

    For an example of what I mean, try this:

    c1 = Card("H", "A")
    c2 = Card("H", "A")
    
    print c1 == c2 # False!
    

    To fix this, you need to add the __eq__ method to your Card class (and probably the __ne__ method too, so you’ll be able to use != tests). Here’s a possible implementation:

    def __eq__(self, other):
        return self.suit == other.suit and self.rank == other rank
    
    def __ne__(self, other):
        return not self == other
    

    There’s one other thing I’d like to point out (unrelated to your question). Your Card class has “getter” methods for the suit and rank. Those are usually unnecessary in Python, where you can generally program everything using public variables at first. That is, anything that currently calls card.get_suit should just access card.suit instead.

    In less common situation where you need to do complicated things in response to variable access (like calculating certain values when they’re requested, or preventing certain values from being assigned), you can put a Property instance in the class (usually as a decorator to a function), and external code can still access it just as if it was still a public variable. Code with lots of getters is common in other programming languages which can’t switch between regular variables and Properties like Python can.

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

Sidebar

Related Questions

In Ruby, there's Modules and you can extend a class by mixing-in the module.
I can extend an inner class/trait inside the outer class or inside a class
I want button in vertically, for that i can extend the height and shrink
Django view points to a function, which can be a problem if you want
I can see in PHP 5.3.2 there is an ArrayObject class. Is it possible
So what I want to do is extend the existing vector class in my
In Java you can define generic class that accepts only types that extend a
I was wondering whether or not I can extend the Enum type in C#
I've a web page, where user can extend the session using AJAX call to
Can I extend WPF commands routing in a way so it would first check

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.