3 ms·
Default values for keywords are saved as part of the function signature itself, at function definition time. Without this it’s not clear how you’d introspect t
by orf 5y ago
Default values for keywords are saved as part of the function signature itself, at function definition time.
Without this it’s not clear how you’d introspect the default kwarg values of a function? Would they be computed every time the function is inspected? If they are lazy then what if the default value is expensive to compute or has side effects?
So I would not say it’s a horrendous decision, it’s a clear trade off that IMO fits quite well in the Python ethos - everything is an object, and everything can be introspected.
- maple3142 5y agoIt might be confusing as it is different from some other languages, such as C++ and JavaScript. They both evaluate default parameter when the function are called.
- orf 5y agoThe difference is neither of these can introspect a functions arguments at runtime. C++ probably doesn’t need to, as it can do it at compile time with templates, but therein lies the difference. Not to say that it can’t is not confusing (at least to begin with), but it’s not an oversight. It’s a conscious choice and I think the only safe one you can make.