Updated March 31, 2023
Introduction to Python References
A reference in python means a different name for a memory location that has been associated. This means an entity allocated with some memory will be referred to or referenced with a different name other than the actual name of the memory. The concept of references expands upon all memory capturing elements like variables, objects and even for items.
How do References work in Python?
The process of referencing in python acts in a below-given manner when one variable is associated with a value or object, and that variable is reassigned to a different variable, then altering the reference of the primary variable will not have any impact on the reference of the secondarily declared variable. this means the variable which was secondarily declared will be steadily pointing to the initially referenced value. this is how variable references works in python. the below example depicts it perfectly,
Example:
Code:
Primary_variable = 5
print("Value of Primary variable: ",Primary_variable)
Secondary_variable = Primary_variable
print("Value of Secondary variable: ",Secondary_variable)
Primary_variable = 10
print("Value of Primary_variable after rebinding: ",Primary_variable)
print("Value of Secondary variable after rebinding: ",Secondary_variable)
Output:
Explanation:
In this example, two variables are used: the primary and the secondary variables; the primary variable is associated with a value of 5 initially. The reference of the primary variable is extended to the secondary variable by associating the primary variable to the second variable. Next, the primary variable is associated with a different value or object. then printing both the variables in the console shows the primary variable being impacted with the new object value, and there is no impact for the secondary variable, and it yet holds the reference to the initial value for which it was assigned.
Types of Python References
Below are the types of Python References:
1. Variable References
Since python does not hold any declarations for its variables, the process of generating a variable is achieved by referencing a name to some entity. this process of associating a name to a value is call binding, and this binding of a variable can also be revoked by means of resetting the variable name to a different entity or a nonexistent value. using the statement of ‘del’ will get the references of that specific variable unbounded. The process of regaining unbounded connectivity is termed as rebinding in python. there is also a different process where all references of the object will be removed, which is mentioned as garbage collection.
The getrefcount() method of the sys library can be used for determining the number of references associated with an object in python. the count of references is calculated by using the number of times the object is used in bytecode and in all previous parts of the code.
Example:
Code:
import sys
Primary_variable = 5
print("Value of Primary variable: ",id(Primary_variable))
Secondary_variable = Primary_variable
print("Value of Secondary variable: ",id(Secondary_variable))
Primary_variable = 10
print("Value of Primary_variable after rebinding: ",id(Primary_variable))
print("Value of Secondary variable after rebinding: ",id(Secondary_variable))
print("Reference count of primary variable:", sys.getrefcount(Primary_variable))
print("Reference count of Secondary variable:",sys.getrefcount(Secondary_variable))
Output:
Explanation:
In this example, again, those two variables are used, namely the primary and the secondary variable; the primary variable is associated with a value of 5 initially. Then, the primary variable’s reference is extended upon to the secondary variable by associating the primary variable to the secondary variable. Next, the primary variable is associated with a different value or object. But the difference here from the above example is that while printing onto the console, we use the id() function to present the memory associated with each of the variables and also additionally, we have used the getrefcount() variable is used to represent the reference count value for both the primary and the secondary variable which is being used. we can notice that the reference or address value associated with the secondary variable is never changed throughout the program.
2. Object References
More specifically to mention the attributes and items are very closely related. the attributes differ from the items by means through which they are referenced. from the object perspective, it will be denoted with an object name and followed by a period (.) and then continues with an attribute name. whereas from the item perspective, the item element holds an item name followed by a square brace [], the braces hold the key which is referenced. virtually mentioning every element in python is an object of a specific class or type.
Example
Code:
import sys
print("Memeory Value of object: ",id(500))
print("References of the object: ",sys.getrefcount(500))
Output:
Explanation:
In this example, an object value of ‘500’ is used. the value ‘500’ will be mentioned to be an object of a class integer. The reference count and the integer object’s memory values are printed in the console, so this explains how references are associated with an object entity.
Accessing nonexistent References:
In python, the usual issue is trying to reach a memory address while which is actually not been associated. some usual instances are a specific variable name that may be unbounded from its reference, or the same may occur for a specific attribute name, or the index of an item that falls above its reference could be tried to each. this kind of errors is not captured during compilations because the compilation process does not consider these scenarios on its execution. So errors of these nonexistent references can be determined only at the time of execution. So as per python execution, the process of accessing a nonexistent variable, attribute, or item, just like any other semantic error, raises an exception.
Conclusion
The process of associating references is a primary content for any programming language, and from a python perspective, this has been very flexibly implemented in python. this concept is very similar to pointers here from python perception; its complexity is reduced by introducing a large number of python-oriented functions or methods. As mentioned above, these sophisticated techniques make python the most flexible language to program upon.
Recommended Articles
This is a guide to Python References. Here we discuss a brief overview of Python References and different types and their Examples, and its Code Implementation. You can also go through our other suggested articles to learn more –