Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Multiplicity

00:00 In the previous lesson, I showed you the different kinds of actions you could use to control how arguments are stored. In this lesson, I’ll be talking about controlling how many parameters there are for your arguments.

00:13 You use the nargs parameter when creating an argument to control how many things are consumed. But just what do I mean by that? Well, if you’re using it with a positional argument, the value of nargs is the count of the number of arguments to process and store as a list. If you’re using an optional flag, nargs specifies how many parameters to expect after the flag.

00:36 It is also stored as a list. Do you feel lucky, punk? Remember back when I was on the waterfront, and the program crashed because I gave two few -w arguments? This fixes that. Dirty Harry here uses the nargs parameter to specify how many -ws to expect. 3, in this case. I feel lucky. Do you feel lucky?

01:07 Note the difference from On the Waterfront. I’m not giving three -ws. I’m giving a single -w that expects three values.

01:17 On line 6, I was explicit about using the store action so you could see what I was doing was something different from the Waterfront example, which used append. But remember, store is the default, so I didn’t really need to do that.

01:39 As nargs was set to 3, if I try to give anything more or less than that, I’ll get an error.

01:49 And the help message is explicit, telling you you need to give three words.

01:57 Okay, from overly violent cops to rocket ships. Smooth transition there, huh? This script does the blastoff countdown, and you get to specify how often the controller says T-minus.

02:11 This time I’m using a positional argument. The * (star) value to nargs indicates that this script takes zero or more arguments, essentially consuming all of the positional arguments. Paging down … The rest of the script prints out the countdown, and if the current line of the count is in the tee argument list, then T-minus gets printed before the number. Total aside, but Cape Kennedy is an amazing place. If you ever find yourself in Florida, it is well worth the visit. Just bring the extra-powerful bug spray.

02:44 Evidently, the mosquitoes didn’t get the message that they should leave after a rocket pad was built on their swamp.

02:54 And there I’ve given the two arguments, which as the type was int, were stored as numbers. And the T-minus was injected at countdown 10 and 5. Let’s try it again.

03:08 As nargs was *, you can give zero or more arguments. In this case, I gave zero. Then you get just the regular countdown. Now I have a strong desire to go watch For All Mankind.

03:22 From spaceships to nineteen-thirties black-and-white horror films, nobody can take my king of the segue crown. Line 8 is just a regular old positional argument.

03:34 Line 9 sets nargs to the REMAINDER constant. This says to consume whatever is left of the positional arguments. In this case, this would be no different than *, but there are some corner cases with very complex parsing where it might be cleaner to use this instead. This script has three sentences, which it prints out interjected with the noise argument.

03:56 Then, whatever is left on the command line gets printed out at the end. Remember that multiple arguments get stored as a list. So to print this out as a single line, it needs to be joined.

04:08 Get your wooden stake at the ready.

04:15 In this case, noise was set to *bleh*. How appropriate. As there were no other arguments given, extra is an empty list. That’s why the blank line happened.

04:25 Joining the empty list results in an empty string. Print an empty string, and you get just the newline. I’ll call it again with some extra this time.

04:40 And there you go. Bela Lugosi would be proud. Insert your own obscure reference to the band Bahaus here. There are a couple more things you can set nargs to.

04:54 You’ve seen using a number and a *. You can also use a ?, which means zero or one arguments. Or there is +, which means one or more arguments.

05:07 If you’re a regular expression person, the choice of these symbols will be meaningful. If you’re not a regular expression person, good for you. They’re messy.

05:16 And of course, you’ve seen the constant, which pretty much means the same as *. Next up, a mishmash of less common features.

Become a Member to join the conversation.