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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:05:32+00:00 2026-05-31T19:05:32+00:00

This is really two questions: Why isn’t the membership operator (__contains__) ever being called?

  • 0

This is really two questions:

  1. Why isn’t the membership operator (__contains__) ever being called?

  2. Why is D in nodeList, but not in nodeSet?

My goal is for D to be “in” both nodeList and nodeSet, because it has the same loc as A.

class Node(object):
    def __init__(self, loc):
        self.loc = loc

    def __eq__(self, other):
        print "eq: self.getLoc(): {}, other.getLoc(): {}".format(self.getLoc(),     other.getLoc())
        if self.getLoc() == other.getLoc():
            return True
        return False

    def __contains__(self, other):
        print "contains: self.getLoc(): {}, other.getLoc(): {}".format(self.getLoc(),  other.getLoc())
        if self.getLoc() == other.getLoc():

            return True
        return False

    def setLoc(self, loc):
        self.loc = loc

    def getLoc(self):
        return self.loc


if __name__ == "__main__":
    A = Node((1,1))
    B = Node((2,2))
    C = Node((3,3))

    D = Node((1,1))

    nodeList = [A, B, C]
    nodeSet = set()
    nodeSet.add(A)
    nodeSet.add(B)
    nodeSet.add(C)

    print "A in nodeList: {}".format(A in nodeList)
    print "A in nodeSet: {}".format(A in nodeSet)
    print "D in nodeList: {}".format(D in nodeList)
    print "D in nodeSet: {}".format(D in nodeSet)

This returns True, True, True, False. Apparently, the __contains__ operator is never called. I would like it to return True, True, True, True.

Any other critiques of my code are of course welcome, as I am a python beginner.

  • 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-31T19:05:34+00:00Added an answer on May 31, 2026 at 7:05 pm

    See the documentation re __hash__() – in short:

    [I]f [a class] defines __cmp__() or __eq__() but not __hash__(),
    its instances will not be usable in hashed collections.

    A set is a hashed collection. You’ll want to make sure you implement Node.__hash__()

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

Sidebar

Related Questions

This consists of two questions: Is MySQL's timestamp field really faster than datetime field
Hopefully this isn't a redundant question--I'm not really sure how to word it. Let's
This really has my stumped today. I'm sure its simple, but... Here is my
This really seems like a bug to me, but perhaps some databinding gurus can
This really puzzled for hours, I searched all over the internet, but got no
First of all, my question isn't really specific to C# or XNA, but my
This is really a two part question, since I don't fully understand how these
I guess this question isn't only specific to YUI, but that's the JS library
This might seem like a silly question, but after asking some questions on stackoverflow
This question has been discussed in two blog posts ( http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/ , http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/ ),

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.