You may not always know how many arguments will be passed to a given function. Variable argument lists provide the ability to pass an arbitrary number of arguments to a function.
- [Instructor] Python functions allow variable length, argument lists like the variadic arguments in C and other languages. Here in Komodo I've opened a working copy of args.py from chapter seven of the exercise files. And you'll notice down here, in our definition for our function, we have a variable name with an asterisk before it. This is the variable length argument list. And you'll notice that we can treat it like a sequence, it's actually a tuple.
So I check and see if the length is greater than zero. Remember that zero is false, anything greater than zero is one, and if it is then I print each of the items in the list, otherwise, I just print meow. And so when I run this you notice we get the three arguments that we had passed it. And if I call without those arguments, we just get meow with a period, just as it is here in this print argument. It's traditional to name your list args, A-R-G-S, and I recommend that you follow that tradition.
That way, when people are reading your code, it's obvious what you're doing. And of course you can call this with however many of these you want. And it will print however many you want. It can be just one or it can be many. This feature is useful for situations where you want a function that may be called with different numbers of arguments. You may also call the same function with a prepared list like this.
And so if I copy this, that'll give us a tuple. And so if we have a tuple or a list or some other collection, I can call it by putting an asterisk before the variable name, and we'll run it and it gets exactly the same results. That'll simply pass a reference to the same object. List arguments are simple to use. They're useful when you need a function that may have different numbers of arguments, and we'll see many examples of this throughout the 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.