- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we assign a reference to a variable in Python?
Concept of variable in Python is different from C/C++. In C/C++, variable is a named location in memory. Even if value of one is assigned to another, it creates a copy in another location.
int x=5; int y=x;
For example in C++, the & operator returns address of the declared variable.
This will print different address of x and y even though both contain same value. You can create an alias to it by storing the address in a reference variable
int x=5; int &y=x; y=10; cout<<x<<y;
This shows both variables having 10. Here, y is a reference to x. Hence they can be interchangeably used.
However, in Python, variable is just a name given to an object in the memory. Even if we assign its value to another variable, both are in fact referring to same object in memory. This can be verified by id() function.
>>> x=5 >>> y=x >>> id(x), id(y) (1486402752, 1486402752)
It therefore is clear that in Python, we can not create reference to a variable.
- How to assign a reference to a variable in C#
- How can we assign a bit value as a number to a user variable?
- Reference assignment operator in PHP to assign a reference?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- How to assign multiple values to a same variable in Python?
- How do I assign a dictionary value to a variable in Python?
- What is a reference variable in C++?
- Defining a variable reference in SAP ABAP
- Can we assign values to final arrays in Java?
- Is it possible to assign a reference to "this" in java?
- How do we assign a value to several variables simultaneously in Python?
- Can we declare a static variable within a method in java?
- Can we cast reference variables in Java?
- How to assign the result of a MySQL query into a variable?