6 ms·
or as Python: shutil.copy(src='foo', dst='bar') That's straight out of stdlib, btw.
by toyg 7y ago
or as Python:
shutil.copy(src='foo', dst='bar')
That's straight out of stdlib, btw.
- echelon 7y agoThose are keyword args and are not specific to just Python. :) The parent post outlines many techniques, including this. Some are more apt than others depending on your language of choice. I would argue that there's another option of passing a strongly typed struct for args, and that the JavaScript and Ruby solution of dicts falls between this and kwargs. I'm fond of properly designed fluent APIs, myself. While on the subject of homogenous argument types, especially bad are the APIs that not only accept the same types, but have order semantics that differ between functions within the same library. PHP used to be a notoriously bad example of this with respect to their needle and haystack search parameters. (I think they fixed it?)
- toyg 7y agoYeah, I was just showing that in Python keyword arguments are first-class in every respect, which is not that common. The "JS solution" for example is not a solution, but only a convention: the actual interface is just expecting an object, and there is no explicit check on the validity of such object or its contents. That is nowhere near "strongly typed", if anything it's weaker than average.
- thelazydogsback 7y ago> The "JS solution" for example is not a solution, but only a convention: the actual interface is just expecting an object, Well if it uses de-structuring syntax, then it can be type-checked.
- tomjakubowski 7y agoDe-structuring syntax isn't required, this is just as easily type-checked: interface CopyArgs { src: string; dst: string; } function copy(args: CopyArgs) { exec(["cp", args.src, args.dst]) }
- thelazydogsback 7y agoSure, if you want to get all interfacy - but pretty soon it'll be Java :)
- davedx 7y agoHah I thought that initially, but TypeScript interfaces are simpler (and usually co-sited) compared to their OO cousins. I find them a pleasure to use.
- RobertKerans 7y agoIt can, manually (as it can for a non-destructured object), but it has no typing guarantees - the function will still happily be interpreted and run as long as _any_ object is given as an argument.
- gnaritas 7y ago> I'm fond of properly designed fluent APIs, myself. Fluent API's are a symptom of a missing language feature, a cascade operator as found in Smalltalk. anObject methodOne: a; methodTwo: b; methodThree: c. ; does syntactically what returning this does in a fluent API without hijacking the return type of the method to do it and allows you to do it on any method any time without it needed to be designed for use that way.
- ambicapter 7y ago> (I think they fixed it?) No, they put out a conf talk that stated that there _was_ a logic to it, you just needed it to be explained to you.
- timw4mail 7y agoReally, most of the relevant PHP functions are just thin wrappers around the C functions with the same name. But nobody talks about how nonsensical C function names are.
- Waterluvian 7y agoI love this part of Python. I can choose to include the argument names if I think they tell a more clear story.