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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:51:51+00:00 2026-06-06T13:51:51+00:00

In the code: y = 7 x = y x = 8 Now, y

  • 0

In the code:

y = 7
x = y
x = 8

Now, y will be 7 and x will be 8. But actually I wan to change y. Can I assign the reference of y and do that?

For example, in C++ the same thing can be achieved as:

int y = 8;
int &x = y;
x = 9;

Now both y & x will be 9

  • 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-06T13:51:54+00:00Added an answer on June 6, 2026 at 1:51 pm

    No, you cannot. As other answer point out, you can (ab?)use aliasing of mutable objects to achieve a similar effect. However, that’s not the same thing as C++ references, and I want to explain what actually happens to avoid any misconceptions.

    You see, in C++ (and other languages), a variable (and object fields, and entries in collections, etc.) is a storage location and you write a value (for instance, an integer, an object, or a pointer) to that location. In this model, references are an alias for a storage location (of any kind) – when you assign to a non-reference variable, you copy a value (even if it’s just a pointer, it’s still a value) to the storage location; when you assign to a reference, you copy to a storage location somewhere else. Note that you cannot change a reference itself – once it is bound (and it has to as soon as you create one) all assignments to it alter not the reference but whatever is referred to.

    In Python (and other languages), a variable (and object fields, and entries in collections, etc.) is a just a name. Values are somewhere else (e.g. sprinkled all over the heap), and a variable refers (not in the sense of C++ references, more like a pointer minus the pointer arithmetic) to a value. Multiple names can refer to the same value (which is generally a good thing). Python (and other languages) calls whatever is needed to refer to a value a reference, despite being pretty unrelated to things like C++ references and pass-by-reference. Assigning to a variable (or object field, or …) simply makes it refer to another value. The whole model of storage locations does not apply to Python, the programmer never handles storage locations for values. All he stores and shuffles around are Python references, and those are not values in Python, so they cannot be target of other Python references.

    All of this is independent of mutability of the value – it’s the same for ints and lists, for instance. You cannot take a variable that refers to either, and overwrite the object it points to. You can only tell the object to modify parts of itself – say, change some reference it contains.

    Is this a more restrictive model? Perhaps, but it’s powerful enough most of the time. And when it isn’t you can work around it, either with a custom class like the one given below, or (equivalent, but less obvious) a single-element collection.

    class Reference:
        def __init__(self, val):
            self._value = val # just refers to val, no copy
    
        def get(self):
            return self._value
    
        def set(self, val):
            self._value = val
    

    That still won’t allow you to alias a “regular” variable or object field, but you can have multiple variables referring to the same Reference object (ditto for the mutable-singleton-collection alternative). You just have to be careful to always use .get()/.set() (or [0]).

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

Sidebar

Related Questions

I have this code ,now can anyone reply s what happens to the lock
I got no errors in the code now, but it doesn't seem to work.
I have some code right now that executes when a link is clicked. The
The javascript code I now have can toggle a single row of data. Whether
I have a job right now that will never quit. It involves running a
I'm using this code now echo $form->input('username'); How do I make sure the label
EDIT: This code now works correctly, I only left it in case someone finds
I'm developing a OpenGL based simulation in C++. I'm optmizing my code now and
I have a page which has many event handlers. The code now reached 1000+
this is my code for now: SELECT id, number FROM Media WHERE user =

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.