-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestbyref.py
More file actions
executable file
·38 lines (29 loc) · 1.21 KB
/
testbyref.py
File metadata and controls
executable file
·38 lines (29 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
__author__ = 'bjcheny'
# http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference
def try_to_change_list_content(the_list):
print 'got', the_list
the_list.append('four')
print 'changed to', the_list
def try_to_change_list_reference(the_list):
print 'got', the_list
the_list = ['and', 'we', 'can', 'not', 'lie']
print 'set to', the_list
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
def main():
outer_list = ['one', 'two', 'three']
print 'before, outer_list = ', outer_list
try_to_change_list_content(outer_list)
print 'after, outer_list =', outer_list
outer_list = ['we', 'like', 'proper', 'English']
print 'before, outer_list =', outer_list
try_to_change_list_reference(outer_list) # The the_list was a copy of the outer_list reference
print 'after, outer_list =', outer_list
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
if __name__ == '__main__':
main()