3 ms·
But in this instance, Python's dumb evaluation evaluation strategy for default expressions makes your code unnecessary, because you could just have: def fro
by kazinator 4y ago
But 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.