Do you know how to force keyword arguments, create a function decorator, create anonymous functions, or unpack an array or dictionary into a function’s arguments? Here are four advanced tricks regarding Python functions.

1. Forced Keyword Arguments

Keyword arguments have a number of advantages:

  • You’re not forced into a particular order in which you supply your arguments. The name matters — not the position.
  • Keyword arguments provide clarity. Without looking up the function itself, you can often guess what the argument is used for by looking at the names.

That’s nice, but you probably already knew these things. What you might not know is that you can also force keyword arguments. The details are described in PEP 3202, but it comes down to using an asterisk before the arguments you want to force as keyword arguments. Or, before everything, forcing all arguments to be keyword arguments:

#programming #python #python3 #data-science

4 Advanced Tricks With Python Functions You Might Not Know
2.75 GEEK