Is it good practice to return an object that is passed by reference and manipulated in a function?
Which of these two approaches is more pythonic? Not returning a manipulated object in place or to return it:
def append42(mylist: list) -> None:
mylist.append(42)
def append42(mylist: list) -> list:
mylist.append(42)
return mylist
The latter looks nicer to me, but it is ambiguous as the function can be used in two different ways to get the same result, e.g.:
mylist = [1, 2, 3]
append42(mylist)
print(mylist)
>>> [1, 2, 3, 42]
or
mylist = [1, 2, 3]
stillmylist = append42(mylist)
assert stillmylist == mylist
print(stillmylist)
>>> [1, 2, 3, 42]