python - How do I pass a variable by reference? -
the python documentation seems unclear whether parameters passed reference or value, , following code produces unchanged value 'original'
class passbyreference: def __init__(self): self.variable = 'original' self.change(self.variable) print(self.variable) def change(self, var): var = 'changed'
is there can pass variable actual reference?
arguments passed assignment. rationale behind twofold:
- the parameter passed in reference object (but reference passed value)
- some data types mutable, others aren't
so:
if pass mutable object method, method gets reference same object , can mutate heart's delight, if rebind reference in method, outer scope know nothing it, , after you're done, outer reference still point @ original object.
if pass immutable object method, still can't rebind outer reference, , can't mutate object.
to make more clear, let's have examples.
list - mutable type
let's try modify list passed method:
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)
output:
before, outer_list = ['one', 'two', 'three'] got ['one', 'two', 'three'] changed ['one', 'two', 'three', 'four'] after, outer_list = ['one', 'two', 'three', 'four']
since parameter passed in reference outer_list
, not copy of it, can use mutating list methods change , have changes reflected in outer scope.
now let's see happens when try change reference passed in parameter:
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)
output:
before, outer_list = ['we', 'like', 'proper', 'english'] got ['we', 'like', 'proper', 'english'] set ['and', 'we', 'can', 'not', 'lie'] after, outer_list = ['we', 'like', 'proper', 'english']
since the_list
parameter passed value, assigning new list had no effect code outside method see. the_list
copy of outer_list
reference, , had the_list
point new list, there no way change outer_list
pointed.
string - immutable type
it's immutable, there's nothing can change contents of string
now, let's try change reference
def try_to_change_string_reference(the_string): print('got', the_string) the_string = 'in kingdom sea' print('set to', the_string) outer_string = 'it many , many year ago' print('before, outer_string =', outer_string) try_to_change_string_reference(outer_string) print('after, outer_string =', outer_string)
output:
before, outer_string = many , many year ago got many , many year ago set in kingdom sea after, outer_string = many , many year ago
again, since the_string
parameter passed value, assigning new string had no effect code outside method see. the_string
copy of outer_string
reference, , had the_string
point new string, there no way change outer_string
pointed.
i hope clears things little.
edit: it's been noted doesn't answer question @david asked, "is there can pass variable actual reference?". let's work on that.
how around this?
as @andrea's answer shows, return new value. doesn't change way things passed in, let information want out:
def return_a_whole_new_string(the_string): new_string = something_to_do_with_the_old_string(the_string) return new_string # call my_string = return_a_whole_new_string(my_string)
if wanted avoid using return value, create class hold value , pass function or use existing class, list:
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 # call wrapper = [my_string] use_a_wrapper_to_simulate_pass_by_reference(wrapper) do_something_with(wrapper[0])
although seems little cumbersome.
Comments
Post a Comment