5) b. Keyword Arguments

Imagine that you are developing a graphics library for other people to use and you use a function definition that looks something like this.

 def draw_rectangle(x, y, h, w, r, g, b): ...

Being a good developer you create documentation of your library that contains several examples of how to use it. One of the example might contain something like this.

 draw_rectangle(100, 20, 500, 700, 10, 10, 10)

Now imagine that someone takes a look at your code and tries to understand it. But there are so many numbers used in one statement and it is not obvious what each of them means.

As a user you should always look for library documentation or the source code in cases like this. And when you see the names of the parameters it is a lot easier to understand the meaning behind them.

But how can you make it easier for yourself to use this code?

Instead of just putting the numbers in the right order you can actually specify the argument by its name when you call the function.

 draw_rectangle(x=100, y=20, h=500, w=700, r=10, g=10, b=10)

Now when you look at your code its meaning is a lot clearer.