4 ms·
This is easily accomplished in python: _DEFAULT = object() # bonus points: make this reload safe def frobnicate(bar=_DEFAULT): if bar is _DEFA
by patrec 4y ago
This is easily accomplished in python:
_DEFAULT = object() # bonus points: make this reload safe
def frobnicate(bar=_DEFAULT):
if bar is _DEFAULT:
...
But really the main reason this comes up is because python went with the dumb decision to evaluate default args at defintion and not call time (unlike, say, common lisp etc.).
- kazinator 4y agoBut in this instance, Python's dumb evaluation evaluation strategy for default expressions makes your code unnecessary, because you could just have: def frobnicate(bar = object()): # object() evaluated once, at definition time Python behaves as if by a transformation to: __hidden_global_42 = object(); # evaluation at definition time def frobnicate(bar = __hidden_global_42): which is exactly what you have done manually.
- DonHopkins 4y agoBut then how does the body of the frobnicate function tell if bar is the default sentinel object() value or not (if bar is ???), without somehow using reflection to fish out a reference to the default value of bar from the function definition? Or is there an easy and efficient way to do that? Add yet another optional-but-not-meant-to-be-passed parameter bar_default=bar maybe? Yuck.
- kazinator 4y agoLooks like you can get at that value in frobnicate.__defaults__. Having spread that, I have to go wash my hands now.