To reference a variable

Asked 2 years ago, Updated 2 years ago, 100 views

It seems that the Python document does not clearly state whether variables are passed by reference or by value when they are passed by parameters. When you turn the code below, the variable value does not change and it remains 'Original', so it seems to pass it to the value.

class PassByReference:
    def __init__(self):
        self.variable = 'Original'
        self.Change(self.variable)
        print self.variable

    def Change(self, var):
        var = 'Changed'

Is there any way to pass a variable to a function as a reference?

argument-passing pass-by-reference reference python

2022-09-22 21:32

1 Answers

The parameters are passed as assignment values.. The logical basis for this is the same as the following two:

Therefore:

If you look at the example below, you'll understand it more clearly.

Let's modify the list handed over by the function:

def try_to_change_list_contents(the_list):
    print 'got', the_list
    the_list.append('four')
    print 'changed to', the_list

outer_list = ['one', 'two', 'three']

print 'before, outer_list =', outer_list
try_to_change_list_contents(outer_list)
print 'after, outer_list =', outer_list

Result:

before, outer_list = ['one', 'two', 'three']
got ['one', 'two', 'three']
changed to ['one', 'two', 'three', 'four']
after, outer_list = ['one', 'two', 'three', 'four']

Because the parameter is a reference value rather than a copy of outer_list, you can replace the value of the list handed over to the function, and the changed value also applies outside the function.

This time, let's change the referenced value itself:

def try_to_change_list_reference(the_list):
    print 'got', the_list
    the_list = ['and', 'we', 'can', 'not', 'lie']
    print 'set to', the_list

outer_list = ['we', 'like', 'proper', 'English']

print 'before, outer_list =', outer_list
try_to_change_list_reference(outer_list)
print 'after, outer_list =', outer_list

Result:

before, outer_list = ['we', 'like', 'proper', 'English']
got ['we', 'like', 'proper', 'English']
set to ['and', 'we', 'can', 'not', 'lie']
after, outer_list = ['we', 'like', 'proper', 'English']

Since the parameter the_list has been passed as a value, you can see that allocating a new list has no effect but on the function. In other words, the_list was a copy of the reference value of outer_list, and changing it to point to a new list does not change the value that outer_list points to.

This type is invariant. So there's nothing you can do about the value of the string.

Let's change the reference value itself:

def try_to_change_string_reference(the_string):
    print 'got', the_string
    the_string = 'In a kingdom by the sea'
    print 'set to', the_string

outer_string = 'It was many and many a year ago'

print 'before, outer_string =', outer_string
try_to_change_string_reference(outer_string)
print 'after, outer_string =', outer_string

Result:

before, outer_string = It was many and many a year ago
got It was many and many a year ago
set to In a kingdom by the sea
after, outer_string = It was many and many a year ago

Similarly, since the parameter the_string has been passed as a value, you can see that allocating a new list has no effect but on the function. the_string was a copy of the reference value of outer_string, but changing it to point to a new list does not change the value itself that outer_string points to.

I hope that this example helped us understand a little bit more clearly.

The method is to have the function return a new value. This method does not change the way parameters are handed over, but it can give information about the values that are deformed within the function.

def return_a_whole_new_string(the_string):
    new_string = something_to_do_with_the_old_string(the_string)
    return new_string

# You can use it like this.
my_string = return_a_whole_new_string(my_string)

If you don't want to use the return value, you can also create a class that contains the value, such as a list, and hand it over to the function.

def use_a_wrapper_to_simulate_pass_by_reference(stuff_to_change):
    new_string = something_to_do_with_the_old_string(stuff_to_change[0])
    stuff_to_change[0] = new_string

# In this case, you can use it as follows.
wrapper = [my_string]
use_a_wrapper_to_simulate_pass_by_reference(wrapper)

do_something_with(wrapper[0])

Of course, this method is a little messy.


2022-09-22 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.