3 ms·
I think the example is indeed contrived - you said it yourself! To me it doesn't illustrate the problem with default parameters specifically. For example it s
by aerhardt 4y ago
I think the example is indeed contrived - you said it yourself!
To me it doesn't illustrate the problem with default parameters specifically.
For example it shows that the programmer doesn't know dependency injection and first-class functions. Printing could be passed in as a function param, but then it might actually be sensible to provide a default callable (eg, print), depending on how the greet function is going to be called.
Language seems to be a perfectly sensible thing to have a default on.
Then, and_return... That is, like, super contrived, man... I mean if a programmer doesn't know that a function caller can simply call for side effects and ignore the return value, they likely have much bigger problems than their judgement to use defaults or not.
I empathize with your plight though - you're probably a great programmer, and I think it's very difficult for someone who is good at their craft to come up with genuinely but subtly shitty examples.
- gorgoiler 4y agoYou are right — the return thing is very silly. What about this: def greet(…, bow=False): … if bow: take_a_bow() Except imagine take_a_bow() instead as 10 lines of code to perform the post-greeting bow-ceremony. That code takes additional arguments regarding what kind of hand flourish to perform while bowing. The flourish_type has to be an optional argument to greet (because bow is) but inside the bow you have to assert flourish_type is not None because you can’t bow without knowing what flourish to give. I’ve seen some dark stuff over the years. Also: Welcome to HN!
- m3047 4y agodef greet(bow=None): ... if bow: bow() def elsewhere(gesture): def bow(): print(gesture) greet(bow) Wrap that.
- rr808 4y agoOMG please leave your dependency injection in your Java project and keep Python clean with its easy to use list of default parameters.
- aerhardt 4y agoYou can use dependency injection in Python while remaining completely pythonic - nothing in the Python I write resembles Java. Dependency injection is not mutually exclusive with using defaults either, so I'm not sure what we're talking about here.
- NeveHanter 4y agoDI is not a pattern to provide the defaults but a pattern that allows you to separate dependency initialisation from your callable's responsibilities, which often is a complex graph of inner dependencies that you would need to provide/initialise in some way, it also can handle whether you want to have single instance of such dependency for your single call, request scope, thread scope or process scope. Also injecting some defaults from configuration object or some properties/setting file is very nice feature. Also stop being so religious and defensive when somebody mentions things that are not standard in your language of choice as nobody forces you to use this. I was sucessfully building very testable and maintanable codebases (~50 kloc) in Python while also using very small in-house built DI framework and it was subjectively (by me and my collegues) much better than what we had before while we followed standard Python patterns and ways Python frameworks teach you to follow.
- jstx1 4y agoWhat's the point of a DI framework? I never got the point of even thinking about DI explicitly like it's something special. It's so obvious that it hardly deserves to have a name, let alone a framework.
- rr808 4y ago> (by me and my collegues) Can I guess you were all Java developers?
- NeveHanter 4y agoNo, around 6-7 Python developers including me, we've used it in two different projects. Also it's probably worth mentioning that I'm not author of that lib, my die-hard anti Java collegue created it by just reading about DI, consulting with other collegue that was using Spring more-or-less since high school and researching existing DI libraries. I myself did some programming in Java before that, but it was mainly hobbist gamedev, later commercial Android and light Java backend work using Spring (that's where I've seen it used for first time) intermixed with around 7 years of professional Python backend programming in two different companies. Now after 10 years I'm an Java engineer, I've had enough of using dynamic languages to write moderately complex web applications. I still love using Python for REPL, small scripts or prototyping, and I think having things like mypy is great as it takes away much of the burden without being a huge obstacle in some situations where you really need to use duck typing. Also I'm thankful that it teached me early that the debugger is one of the developer's best friends and the best documentation is just reading the code.