Skip to content

Instantly share code, notes, and snippets.

@scandiumby
Last active January 21, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scandiumby/de139e7e231b4dbb6f888c79442205f6 to your computer and use it in GitHub Desktop.
Save scandiumby/de139e7e231b4dbb6f888c79442205f6 to your computer and use it in GitHub Desktop.
Mutable and immutable args for functions in Python3
class AutoInc:
current_index = 0
def __call__(self):
self.current_index += 1
return self.current_index
def append_to_list(alist: list):
alist.append(7)
def del_var(v):
del v
def set_var(v):
v = 5
def append_by_yeild(alist: list):
alist.append(8)
yield
alist.append(9)
if __name__ == '__main__':
list_ = [1, 2]
int_ = 10
list2_ = list_
append_to_list(list_)
del_var(int_)
del_var(list_)
set_var(int_)
i = AutoInc()
print(f'{i()}: {list_=}')
print(f'{i()}: {int_=}')
print(f'{i()}: {list2_=}')
saved_func = append_by_yeild(list2_)
print(f'{i()}: {list_=}')
del list_
next(saved_func)
print(f'{i()}: {list2_=}')
print(f'{i()}: {list_=}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment