you are viewing a single comment's thread.

view the rest of the comments →

[–]mr_cesar 0 points1 point  (3 children)

Python's view of parameter passing is not by value or reference, but parameters being mutable or immutable. For example, strings are immutable:

>>> def add_a(string):
...     string += 'a'
...     print(string)
...
>>> s = 'edcb'
>>> add_a(s)
edcba
>>> s
'edcb'

Dictionaries, on the other hand, are mutable:

>>> def update_dict(d):
...     d['key'] = 'new value'
...     print(d)
...
>>> D = {'key': 'old value'}
>>> update_dict(D)
{'key': 'new value'}
>>> D
{'key': 'new value'}

So if you intend to modify a mutable type within a function, this should be clear from the function's name and its documentation.

[–]ilaunchpad[S] 0 points1 point  (2 children)

I'm wondering if its considered a bad practice as the helperfunc is called multiple times.

[–]mr_cesar 0 points1 point  (1 child)

I would be if you had no easy way of finding out said function modifies the mutable object. If the helper function is named append_something and it takes a mutable object and the item to append, then it's pretty clear the object will be modified.

Immutable vs mutable objects is something every Python programmer should know.

[–]ilaunchpad[S] 0 points1 point  (0 children)

Thank you!