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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:42:59+00:00 2026-05-15T16:42:59+00:00

For some types in Python, the is operator seems to be equivalent to the

  • 0

For some types in Python, the is operator seems to be equivalent to the == operator. For example:

>>> 1 is 1
True
>>> "a spoon" is "a spoon"
True
>>> (1 == 1) is (2 == 2)
True

However, this is not always the case:

>>> [] == []
True
>>> [] is []
False

This makes sense for mutable types such as lists. However, immutable types such as tuples seem to display the same behavior:

>>> (1, 2) == (1, 2)
True
>>> (1, 2) is (1, 2)
False

This raises several questions:

  1. Is the == / is equivalence related to immutability?
  2. Are the behaviors above specified, or an implementation detail?
  3. Most importantly (and basically), how can I know if an assignment will result in a copy of an object being made, or a reference to it being made?

Update:
If assignment is always by reference, why doesn’t the following print 2?:

>>> a = 1
>>> b = a
>>> a = 2
>>> b
1

Why isn’t this equivalent to the following C snippet:

int a = 1;
int *b = &a;
a = 2;
printf("%d\n", *b);

Apologies for the newbness of this question, but I am a Python novice and feel that it is important to understand this. Is there any reading you would recommend to understand these sort of issues?

  • 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-15T16:43:00+00:00Added an answer on May 15, 2026 at 4:43 pm

    The is operator tests if two objects are physically the same, that means if they have the same address in memory. This can also be tested using the id() function:

    >>> a = 1
    >>> b = 1
    >>> a is b
    True
    >>> id(a) == id(b)
    True
    

    The == operator on the other hand, tests for semantical equality. This can also be overridden by custom classes by implementing the __eq__() function. Semantically, two different lists are equal if their elements are all equal, but physically they will be different objects.

    Immutable types like strings and tuples might be pooled by the Python implementation, so that two literal string objects are in fact physically identical. But this does not mean that you can always use is to compare those types, as demonstrated in the following example:

    >>> "foobar" is "foobar"   # The interpreter knows that the string literals are
    True                       # equal and creates only one shared object.
    >>> a = "foobar"
    >>> b = "foobar"
    >>> a is b        # "foobar" comes from the pool, so it is still the same object.
    True
    >>> b = "foo"     # Here, we construct another string "foobar" dynamically that is
    >>> b += "bar"    # physically not the same as the pooled "foobar".
    >>> a == b
    True
    >>> a is b
    False
    

    Assignments in Python always bind the reference to an object to a variable name and never implies a copy.

    UPDATE

    Analogous to C, think of Python variables always being pointers:

    >>> a = 1
    >>> b = a
    >>> a = 2
    >>> b
    1
    

    Roughly equivalent to:

    const int ONE = 1;
    const int TWO = 2;
    
    int *a = &ONE;
    int *b = a;  /* b points to 1 */
    a = &TWO;    /* a points to 2, b still points to 1 */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Get an IWICMetadataQueryReader object and use its GetEnumerator method. With… May 15, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer Try "python ./manage.py shell" and see what you get. Since… May 15, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer You declare variable test inside the loop, so it scope… May 15, 2026 at 6:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.