by vorticalbox · 3y ago · 0 comments
def variable(n:str, nn:str, nnn:str, *, a:int, b:int, c:int)
Anything after,*, is a kwarg.
def sum(*args: int) -> int:
if len(args) == 0:
return 0
return args[0] + sum(*args[1:])GP is suggesting that one should only ever use explicit keyword-only args (anything listed after `*,` in the signature) versus arbitrary keyword args implicit via `**kwargs`.
e.g. (omitting type hints for clarity):
def sum(*args, **arbitrary_kwargs):
...
vs def sum(*args, some_keyword_only_arg):
...
In my opinion if one finds themselves writing code that uses arbitrary kwargs, they've got a design problem.**`a[1:]` returns the sequence of elements that start at index 1. If there is no such element, the list is empty. I don’t see any good reason why this should throw an error.
This could easily conceal the indexing error unless the caller code explicitly checks the length of the returned section.
I understand the logic behind both decisions, but it’s not surprising that people find it inconsistent and unintuitive.
When using slice notation, the return value is a sequence, so returning a zero-length sequence is sufficient to communicate you asked for more items than exist.
It may be surprising, but it almost always leads to more ergonomic code.
print(firstname, lastname)
for example is more readable than print((firstname, lastname))
especially since I would then have to write print((surname,))
to just print a single string.Variadic functions are rather classic, I think Go, Rust, C and JavaScript also have them.
What do you do with your first example if you have a list (generated at runtime, not a static one) to pass to the function? This wouldn't work (imagine the first line is more complicated):
l = (1,2,3)
print(l)The wrapper is usually some shorthand for building a handful of those args or adding some side-effect, while still allowing the caller access to the remaining config options via kwargs.
Here's one example of that in the wild https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot....
[0] actually 3 positional-or-keyword which is even more widely divergent
It's certainly debatable whether doing that is better than passing a single argument whose value is a dict with that same type, but many people do prefer the variadic args/kwargs style.
I would rather:
@dataclass(frozen=True, slots=True)
class VarThings:
n: int
...
def variable(a: VarThings):
...
Than a million args class Employee(NamedTuple):
name: str
id: intA required positional OR kwarg as you’ve done it. Its closer to an optional kwarg if you expand the type declaration to also allow None and set a None default.
But there are times when you want to leave the number and names of kwargs open (one example is for a dynamic wrapper—a function that wraps another function that can be different across invocations.)
subprocess.run() is an example of that. Also check out the functions in manim.
The inability to properly static type kwargs using TypedDict is probably the biggest flaw in Python's type hint system (after the fact that hardly anyone uses it of course).