4 ms·
I twitched a little when I saw lists and dicts included as default arguments in one of the examples: def main( ..., obj: dict = {"even": "dicts"},
by philsnow 4y ago
I twitched a little when I saw lists and dicts included as default arguments in one of the examples:
def main(
...,
obj: dict = {"even": "dicts"},
my_list: list = ["or", "lists!"],
...
has modern python made these less footgun-y? the issue being that the code looks like 'obj' and 'my_list' are as provided in the default arguments every time the function is called, but the defaults are instantiated once and they can be modified inside the function:
def foo(msgs=["hello"]):
print(msgs)
msgs.append("world")
foo() # prints ['hello']
foo() # prints ['hello', 'world']
foo() # prints ['hello', 'world', 'world']
Nonetheless, introspecting on default arguments like that looks like a fine way to turn functions into UI elements if you're going to contrive that those functions aren't going to be re-entered multiple times in the same python runtime.
- animal_spirits 4y agoNope, this is still a "feature" of python and will continue to be