What are the ways to write a function using call by reference?
Answer Posted / chaitanya
Arguments in python are passed as an assignment. This assignment creates an object that has no relationship between an argument name in source and target. The procedure to write the function using call by reference includes:
The tuple result can be returned to the object which called it. The example below shows it:
def function(a, b):
a = 'value'
b = b + 1
# a and b are local variables that are used to assign the new objects
return a, b
# This is the function that is used to return the value stored in b
• The use of global variables allows the function to be called as reference but this is not the safe method to call any function.
• The use of mutable (they are the classes that consists of changeable objects) objects are used to pass the function by reference.
def function(a):
a[0] = 'string'
a[1] = a[1] + 1
# The ‘a’ array give reference to the mutable list and it changes the changes that are shared
args = ['string', 10]
func1(args)
print args[0], args[1]
#This prints the value stored in the array of ‘a’
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
How would you implement inheritance in python?
How do you convert a number into a string?
What does == mean in python?
What is the difference between if and elif in python?
How can you make a python script executable on unix?
What is the best front end for python?
Is set iterable in python?
Is true a keyword in python?
How do you get a list of all the keys in a dictionary in python?
What is the usage of dir() and dir() function in python?
Explain me what is the statement that can be used in python if the program requires no action but requires it syntactically?
Is r similar to python?
How do I watch a file for changes using python?
What is python rest api?
How do I run python in notepad ++?