python - How do I pass a variable by reference? -
the python documentation seems unclear whether parameters passed reference or value, , next 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 info types mutable, others aren'tso:
if pass mutable object method, method gets reference same object , can mutate heart's delight, if rebind reference in method, outer scope know nil 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 create more clear, let's have examples.
list - mutable typelet's seek 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:
class="lang-none prettyprint-override">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 re-create of it, can utilize mutating list methods alter , have changes reflected in outer scope.
now let's see happens when seek alter 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:
class="lang-none prettyprint-override">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
re-create of outer_list
reference, , had the_list
point new list, there no way alter outer_list
pointed.
it's immutable, there's nil can alter contents of string
now, let's seek alter 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:
class="lang-none prettyprint-override">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
re-create of outer_string
reference, , had the_string
point new string, there no way alter outer_string
pointed.
i hope clears things little.
edit: it's been noted doesn't reply question @david asked, "is there can pass variable actual reference?". let's work on that.
how around this?as @andrea's reply shows, homecoming new value. doesn't alter way things passed in, allow info want out:
def return_a_whole_new_string(the_string): new_string = something_to_do_with_the_old_string(the_string) homecoming new_string # phone call my_string = return_a_whole_new_string(my_string)
if wanted avoid using homecoming value, create class hold value , pass function or utilize 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 # phone call wrapper = [my_string] use_a_wrapper_to_simulate_pass_by_reference(wrapper) do_something_with(wrapper[0])
although seems little cumbersome.
python reference pass-by-reference argument-passing
No comments:
Post a Comment