Do you want to assign names to your function arguments? Keyword arguments work like dictionaries, providing keyword names for the arguments passed to a function.
- [Instructor] Key word arguments are like list arguments that are dictionaries instead of tuples. This allows your function to have a variable number of named arguments. Here in Komodo, I've opened a working copy of kwargs.py from chapter seven of the exercise files. And you notice, down here on line seven, the function argument has two asterisks in front of it instead of one, as we had for the list arguments. And it's named kwargs, which stands for key word arguments.
And so when I run this, you'll notice that it says Kitten Buffy says meow, Zilla says grr, Angel says rawr. When we call the function, we have named each of the arguments. Buffy = 'meow', Zilla = 'grr', Angel = 'rawr.' This is the same syntax as we used for dictionary. And, in fact, this is a dictionary. If I wanted to, I could say, x equals and dictionary with those arguments, and then I can pass it x.
And just as I would use one asterisk for a list, I use two asterisks for passing at this dictionary, and our result is exactly the same. Again, it's traditional to name your keyword argument kwargs, you're not required to follow this convention, but it's generally a good idea to follow a convention. So that when people read your code, they'll know exactly what it means. Keyword arguments are powerful and simple. They're useful when you need a function that may have different numbers of named arguments.
And you'll see many examples later in this course.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.