python sucks because: default parameters are only evaluated once.
Also omitted:
my_dict.get('existing_key', foo())
does not short-circuits foo(). That always felt like a bug to me. my_dict['existing_key'] if 'existing_key' in my_dict else foo()
You could also define a `get_lazy` method that does the above, taking a nullary function as its key and evaluating it if the key is missing. But then you would need to wrap your default values in a lambda, which would look absurd for the common use case of calling `get` with some constant default value. Seems unnecessary when the ternary operator works fine. my_dict.get('existing_key') or foo()
This would also easily let you do multiple alternations (much more annoying with the ternary operator). print(player_data['score'] or 'does not exist')
This will print "does not exist" when the score is zero. def foo(bazzes=None):
_baz = bazzes or []
...I use a linter to catch this sort of thing and subclass `collections.namedtuple(...)` whenever I need to at least try to abolish mutability. It's one of the worst things about the language.
[edit] You can argue that there would be use cases for mutable defaults, and as with any edge use case you'd be right in asserting its existence. Unfortunately, I can't think of any solution (typing, changing function argument evaluation rules, etc) that wouldn't fundamentally change the language and break just about every sizable python library in existence. This looseness is baked deep into the language as far as I can see.
- how does it know you're not doing that on purpose?
2) good point.