Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Now in the previous lecture, we learned what functions are, how to define a function and how to call
it.
We also learned that functions can take input like the one that we just created and they can return
input.
But as you can see, the one that we created here, the change Mac function does not return anything
because it doesn't need to.
It only takes interface and a new Mac and it changes the Mac address for the interface.
So in this lecture, let's continue organizing our code.
We're going to create a new function.
And as we do that, we'll also have an example of a function that returns a value.
So first of all, I just want to make this tidier here, and we're going to create our new function
here and we're going to call this new function, get arguments.
Because I want this function to parse the user input and return to me the arguments and the values entered
by the user.
So it's basically, what's this code in here?
Does.
So all we have to do is just copy all of this.
And paste it in my function, but make sure that there is a tab, there is a distance in here before
we paste this so that all of this will be considered as part of the function.
Now I'm going to just delete these blank lines here.
And that's it.
We have our parser, so we have a function called get arguments.
What it does is it creates our parser object.
It adds an option of the interface.
It adds an option of the mark.
And finally it's passing the user input using parser to parse args and storing the values in options
and arguments.
The only problem is options and arguments are only accessible within this function right here.
They're only accessible within this block of code.
So even if we call this function in here at the bottom, the same way that we called our change mark.
You'll see that pie chart is still telling us that options is an unresolved reference.
It's still not going to know what options is because it's only used within the function in here.
Let me show you what I mean.
So I'm going to go down on my program and I'm just going to run the exact same command that we always
run, and you'll see that we're going to get an error.
And it's a good idea to learn how to read these errors and understand what's going wrong, because you'll
definitely get errors in the future.
So you can see that it's telling us that the error is in a file called Mac Changer Dot Pie.
So this is the file that contains my code.
The error is on line 22 and it's listing the line that contains the error and it's given us the error
type, which is a name error.
And the problem is the name options is not defined.
So going up for line 22, we can see that it's even underlined in red here because Python doesn't know
what options is.
We never used it in here.
Each block of code here is separate than the main program.
Even though we're calling get arguments and even though we're setting options and arguments to equal
to the parser dot parse args, this variable is only usable within this block of code.
So in order to capture the value that's in here, what we need to do is I'm going to delete all of this.
I'm actually going to cut it, not delete it, and we're going to say return parser to parse ARGs.
So whenever you get called I want you to execute all the code in here and return parser dot parse args
value to the location where you get called from.
So we're calling all of this on line 21 in here and because we're using this return instruction in here,
get arguments now will return whatever parser dot parse args returns and we can capture it the exact
same way that we capture parser dot parse args by doing options arguments equals get arguments.
Okay.
So all we did is we copied all the code that we used to use to parse the arguments inside the function
called get arguments.
And instead of initializing a variable at the end, we returned the value that the parser that parse
ARGs returns.
Then whenever we call this function, we capture the returned value here using other variables.
So we're using options.
Arguments equals this and then again options is being called here.
Now Python knows what options is equals to and it can run the change mac function.
So here is what's going to happen when we execute the Python program.
It's going to skip all of this.
And the first thing that it's going to do is it's going to run the get argument function.
The get argument function will read all the arguments entered by the user.
It will pass them in here.
And once done with that, it will return the two sets of information, the arguments and the values.
Because we're using the return instruction, whatever that's being returned with, this function is
being captured by options and arguments.
Then we're calling the change mark.
So Python is going to go here.
We're giving it the interface, we're giving it the mark, and then it will execute all of these commands,
which will change my Mac address.
Now let's test this code.
Using the same command.
Let's just set the Mac address to be three three at the end.
Hit enter.
Get the right print message.
And if we do, if config at zero, you'll see that my mac address did actually get changed.
Now, if we look at our code right now, it looks much, much nicer, much more readable.
And it's also reusable because we can reuse any of the functions whenever we want.
So now we have the code for the main program in here, and that's only two lines.
And then all the functionality is encapsulated into two functions.
One of them gets the arguments entered by the user, and the other one changes the MAC address based
on the argument that the user entered.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.