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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:54:36+00:00 2026-05-14T03:54:36+00:00

Is it preferred to do: if x is y: return True or if x

  • 0

Is it preferred to do:

if x is y:
    return True

or

if x == y
    return True

Same thing for “is not”

  • 1 1 Answer
  • 1 View
  • 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-14T03:54:36+00:00Added an answer on May 14, 2026 at 3:54 am

    x is y is different than x == y.

    x is y is true if and only if id(x) == id(y) — that is, x and y have to be one and the same object (with the same ids).

    For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x == y is also True. However, this is not guaranteed in general. Strictly speaking, x == y is true if and only if x.__eq__(y) returns True.

    It is possible to define an object x with a __eq__ method which always returns False, for example, and this would cause x == y to return False, even if x is y.

    So the bottom line is, x is y and x == y are completely different tests.

    Consider this for example:

    In [1]: 0 is False
    Out[1]: False
    
    In [2]: 0 == False
    Out[2]: True
    

    PS. Instead of

    if x is y:
        return True
    else:
        return False
    

    it is more Pythonic to write

    return x is y
    

    And similarly,

    if x == y:
        return True
    else:
        return False
    

    can be replaced with

    return x == y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the preferred way to work with Singleton class in multithreaded environment? Suppose
Which method is preferred for adding a row to a table? var tr =
What is the preferred way of deploying a compojure/sinatra applications? I have multiple sites
Is there a preferred algorithm for converting a 2-digit year into a 4-digit year?
What is the preferred way to build application gui components tree? Instantiate all components
What are the preferred platforms to implement TCP socket communication ? I'm interested in:
What is your preferred approach to serializing BigDecimal in GWT? Are there any clever
Which is the preferred way of defining class properties in Python and why? Is
When is one preferred over the other when searching for nested Divs?
I'm facing the same issue others already posted on SO: On reading objects from

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.