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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:06:51+00:00 2026-05-27T16:06:51+00:00

There is this code: # assignment behaviour for integer a = b = 0

  • 0

There is this code:

# assignment behaviour for integer
a = b = 0
print a, b # prints 0 0
a = 4
print a, b # prints 4 0 - different!

# assignment behaviour for class object
class Klasa:
    def __init__(self, num):
        self.num = num

a = Klasa(2)
b = a
print a.num, b.num # prints 2 2
a.num = 3
print a.num, b.num # prints 3 3 - the same!

Questions:

  1. Why assignment operator works differently for fundamental type and
    class object (for fundamental types it copies by value, for class object it copies by reference)?
  2. How to copy class objects only by value?
  3. How to make references for fundamental types like in C++ int& b = a?
  • 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-27T16:06:51+00:00Added an answer on May 27, 2026 at 4:06 pm

    This is a stumbling block for many Python users. The object reference semantics are different from what C programmers are used to.

    Let’s take the first case. When you say a = b = 0, a new int object is created with value 0 and two references to it are created (one is a and another is b). These two variables point to the same object (the integer which we created). Now, we run a = 4. A new int object of value 4 is created and a is made to point to that. This means, that the number of references to 4 is one and the number of references to 0 has been reduced by one.

    Compare this with a = 4 in C where the area of memory which a “points” to is written to. a = b = 4 in C means that 4 is written to two pieces of memory – one for a and another for b.

    Now the second case, a = Klass(2) creates an object of type Klass, increments its reference count by one and makes a point to it. b = a simply takes what a points to , makes b point to the same thing and increments the reference count of the thing by one. It’s the same as what would happen if you did a = b = Klass(2). Trying to print a.num and b.num are the same since you’re dereferencing the same object and printing an attribute value. You can use the id builtin function to see that the object is the same (id(a) and id(b) will return the same identifier). Now, you change the object by assigning a value to one of it’s attributes. Since a and b point to the same object, you’d expect the change in value to be visible when the object is accessed via a or b. And that’s exactly how it is.

    Now, for the answers to your questions.

    1. The assignment operator doesn’t work differently for these two. All it does is add a reference to the RValue and makes the LValue point to it. It’s always “by reference” (although this term makes more sense in the context of parameter passing than simple assignments).
    2. If you want copies of objects, use the copy module.
    3. As I said in point 1, when you do an assignment, you always shift references. Copying is never done unless you ask for it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is this code: class SomeClass { public: SomeClass(){} SomeClass(SomeClass& b){} SomeClass(SomeClass&b, SomeClass& c){}
There is this code: #include <iostream> class Base { public: Base(){ std::cout << Constructor
Is there anyway this code can be refactored? The only difference is the order
There is this example code, but then it starts talking about millisecond / nanosecond
There is currently this Prototype code that does a PUT: new Ajax.Request(someUrl, { method:
Is there anything wrong with this code? My entity is not getting updated. public
There's something very unsatisfactory about this code: /* Given a command string in which
I have this code so far which perfectly but relies on there being a
Is there an easy way to modify this code so that the target URL
Is there a better way to write this code? I want to show a

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.