-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionArgMutability.py
More file actions
executable file
·53 lines (42 loc) · 1.22 KB
/
FunctionArgMutability.py
File metadata and controls
executable file
·53 lines (42 loc) · 1.22 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 24 18:56:04 2021
@author: maherme
"""
#%%
# Notice the memory address in this example of inmutable object and how this
# object does not change after the function is called.
def process(s):
print('Initial s # = {0}'.format(id(s)))
s = s + ' world'
print('Final s # = {0}'.format(id(s)))
my_var = 'hello'
print('my_var # = {0}'.format(id(my_var)))
process(my_var)
print(my_var)
#%%
# Notice the memory address in this example of mutable object and how this
# object changes after the function is called.
def modify_list(lst):
print('Initial lst # = {0}'.format(id(lst)))
lst.append(100)
print('Final lst # = {0}'.format(id(lst)))
my_list = [1, 2, 3]
print(id(my_list))
modify_list(my_list)
print(id(my_list))
print(my_list)
#%%
# Let's see with a tuple:
# Notice the inmutable element can not change, but the mutable elements inside
# an inmutable element can change.
def modify_tuple(t):
print('Initial t # = {0}'.format(id(t)))
t[0].append(100) # Assume the first element of the tuple is a list
print('Final t # = {0}'.format(id(t)))
my_tuple = ([1, 2], a)
print(id(my_tuple))
modify_tuple(my_tuple)
print(my_tuple)
#%%