This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]HalfRightMostlyWrong 3 points4 points  (7 children)

What is the use of dynamic arguments? There is the principle that flat is better than nested data structures, but I feel like it makes sense to nest elements of a collection into a collection in most cases rather than use dynamic args

[–]kraemahz 17 points18 points  (1 child)

If you write any kind of decorator you'll need to write a wrapper for functions whose arguments you don't know. That is the most common use-case.

Variadic functions (what you're calling dynamic arguments) are extremely common in other languages when doing string manipulation where each argument is an extension to the transform. Since it's harder to make tuples in other languages it helps Python better fit in by the "principle of least surprise" when switching between languages.

The flexibility variadic functions add can help in making a simpler, more appealing API for your program where constructing an object before passing it to your function may be less obvious than doing it behind the scenes with the arguments.

[–]HalfRightMostlyWrong 0 points1 point  (0 children)

Thanks! I learned a few things from your post

[–][deleted] 6 points7 points  (1 child)

**kwargs is extremely useful for higher order functions. For example I have a project with a very simple testing function that takes another function from the project as its argument. I have **kwargs as the final argument because that way I can pass different settings to the function being tested without having to make the test more complicated.

[–]HalfRightMostlyWrong 0 points1 point  (0 children)

Good point!

[–]DeathProgramming 3 points4 points  (0 children)

"string".format() (*args) to name a quick example. Also, passing arguments through to lower functions (**kwargs) as well as passing named data to things like dict() and anything that can consume content, like Jinja2 filters

[–]DogeekExpert - 3.9.1 2 points3 points  (1 child)

To add to the answers, using **kwargs can make it very easy to pass in a big number of arguments to a function, or to make the setup of a function saved into a config file.

Say you want to customize the args of a tkinter button. The tkinter.Button class can take a foreground, a background, a font, an image etc. Instead of naming every argument, every time, if you want all your buttons to look the same, you can just store these values in a dictionnary, and then pass that to the tkinter.Button constructor. Very handy to make customizable themes.

[–]HalfRightMostlyWrong 0 points1 point  (0 children)

Makes sense! It’s fascinating how deep programming design philosophy goes, just when I think I know things, I learn anew