5 ms·
i am a huge fan of this style: def foo(obvious_thing:type, *, modifier:type, modifier2:type, …) Because I find this call incomprehensible: foo(items, 12,
by hprotagonist 5y ago
i am a huge fan of this style:
def foo(obvious_thing:type, *, modifier:type, modifier2:type, …)
Because I find this call incomprehensible:
foo(items, 12, “apple”)
and this immediately better:
foo(items, num=12, kind=“apple”)
and preventing my callers from being able to do the first is a huge win.
- peteradio 5y agoSeems reasonable but when are you actually calling foo like: foo(items, num=12, kind=“apple”) vs: foo(items, num=num, kind=kind) ? Then you might as well get your info from the variable names in the calling method.
- messe 5y agoThere's been some discussion on that[1], with some suggesting the introduction of syntax analogous to f-strings: foo(items, num=, kind=) # equivalent to foo(items, num=num, kind=kind) But as far as I know there hasn't been a formal proposal (I could be wrong, and am happy to be corrected on this). [1]: https://lwn.net/Articles/818129/ https://lwn.net/Articles/818129/
- hprotagonist 5y agothat looks nice! not essential, but nice. I like the similar 3.8+ f-string improvement: x=5 print(f”{x=}”)
- hprotagonist 5y agoand one day someone goofs and foo(items, kind, num) and then everyone is sad again.
- wheelerof4te 5y agoYes, but keyword arguments are a good fallback option. They indicate that you are indeed passing the required arguments. So, the def foo(val, querry=querry): ... doesn't look as funny as you might think.
- DangitBobby 5y agoForcing your consumers to use a style you like is not a "win", it's a power play.
- jackblemming 5y agoThis is completely right. On top of this, most modern IDEs can show you implicit parameter names in the function call if you want. It's a feature you can toggle on or off. Don't force people to consume things how you want because you dont use local variables or have a modern IDE.
- hprotagonist 5y agoi intend it as both, and i’m aware of that fact. you call it “power play”, i call it “having an API for my libraries”; potato, potato.
- DangitBobby 5y agoYou have an API either way.
- joshuamorton 5y agoAlternatively, it's "opinionated", which is often a great selling point for libraries.
- DangitBobby 5y agoWhen it makes hard choices so you don't have to? Absolutely. This isn't a hard choice that has to be made, though. It's an arbitrary choice for style.
- Spivak 5y agoNo, it’s a contract guarantee. It says, except for the first parameter, I make no guarantee on the order of these parameters. def verb(object, **adjectives) is the pythonic way to pass an “options” dict. It doesn’t make sense as positional parameters and makes for a worse API.
- edgyquant 5y agoThis is definitely how I prefer it as well after watching a talk on YouTube by a python core dev who recommended this for easier readability.
- patrick451 5y agoNot a win for your users. It makes your code a pain to use. All things being equal, I would choose a library that was less heavy handed.