Aliasing in PythonWhat is Aliasing?Aliasing is a method to change the name of any data in Python, like a list, function, tuple, etc. When any variable name is already assigned to another variable, it is necessary to alias the data. For instance, some variables are just store references of the actual value. Thus, we need to give a second name to it. We can do aliasing for variables, lists, functions, etc. Aliasing can be defined as giving a name to an existing data type in Python. We create a new variable and assign the reference to the existing data type. The aliasing can be verified by checking the id of the original and aliased data type by using the id( ) function. List AliasingList aliasing is a method to give a second name to an existing list. Firstly, we will declare a list. Then, we declare another variable and assign the original list to the new variable. It will copy the list explicitly and have the same reference ID, i.e., it has the same memory address. We can check the id using the id( ) function. If we try to change any element in any of the lists, it will change in both lists. Let's implement the list aliasing in Python. Example Code: Output: Original List: [7, 8, 15, 99, 44] 2568590629632 List after aliasing: [7, 8, 15, 99, 44] 2568590629632 We declared a list of numbers (original_list) and printed the list with its memory address. Then, we declared another variable (alias_list) and assigned the original list to it. The list name alias_list is the second name for the original_list. Then, printed the aliased list and its memory address. It is seen that both the list has the same data and the memory address. If any changes are made to the original_list, it will also change the alias_list. Variable AliasingVariable Aliasing is a method of giving a second name to a variable in which the value assigned to one variable will be assigned to another variable. In this, a variable is assigned to a value, then another variable is created, and the variable is assigned to it. Both the variables will have the same value and the same memory address. We can check the memory address using the id( ) function. We will understand the aliasing with the help of an example. Example Code: Output: Original Variable: 88 , Memory address: 140719436045832 Aliasing Variable: 88 , Memory address: 140719436045832 We declared a variable original_variable and assigned a value to it. Then, declared another variable (alias_variable) and assigned the original_variable to it. We printed the value of both variables and their memory address. As an output, both have the same name and memory address. Function AliasingIn function aliasing, we create a function, and then, using a different variable name, we assign the existing function. Both the function will have the same value and memory address. The memory address can be checked with the id( ) function. If we change anything in the original function, it will also modify the reference function with another name. Function Aliasing in Python using an example: Example Code: Output: Memory address of funct1: 2863587213856 Memory address of funct2: 2863587213856 10 10 We created a function (funct1) to add two numbers. Then, we created a variable funct2 and assigned funct1 to it. The memory address of both functions is the same, which is calculated with the id( ) function. The value evaluated with both functions is also the same. Next TopicAnalysing-data-in-python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India