0

I always find very user-friendly the methods with multiple predefined parameters such as:

plt.plot([1,2,3], [5,1,2], color = "black", linewidth=10)

In this example the order of the arguments color and linewidth is indifferent, in addition the type of the arguments is undefined (**kwargs). The easiness of use comes with the suggestion of parameters as shown in the picture: Suggested parameters I tried to explore the method .plot() and it lead me into a deep rabbit hole. The question is, how would you implement such a behavior?

nbes
  • 11
  • 1
  • 2
  • https://stackoverflow.com/questions/9539921/how-do-i-create-a-python-function-with-optional-arguments – Esther Jun 21 '22 at 19:22
  • Are you looking for keyword-only parameters? – chepner Jun 21 '22 at 19:22
  • The completion is a feature of whatever editor or IDE you are using, insofar as it inspects the parameter names listed in the signature, not something you define in Python – chepner Jun 21 '22 at 19:24
  • [The Python documentation](https://docs.python.org/3/tutorial/controlflow.html#default-argument-values) has a section on default arguments. – Anonymous12358 Jun 21 '22 at 19:26
  • @Esther I did try to implement something like that, but inmediatly went messy with default values, the order of the parameters, and which params belong to *args and which ones belong to *kwargs. – nbes Jun 21 '22 at 20:12
  • `kwargs` stands for "keyword arguments" and `args` is "arguments", so `**kwargs` gets all the arguments that are called with a keyword (`keyword = 'value'`). You have to put required arguments *first*, then all the not-required ones (ones with default values). When calling the function, you must include all required args. If you skip any optional args, all args afterwards must use keywords. – Esther Jun 21 '22 at 20:26

0 Answers0