So friends, the last thing that I just want to briefly talk to you about is the
concept
of functions within Python.
Now understand, within programming, we have this concept that's known as this,
the dry
concept.
Don't repeat yourself.
So quite simply, let's say you have a piece of code that is really quite
valuable at
something you want to use over and over and over again, you don't want to have
to continually
rewrite the same logic over and over and over again.
This is something you want to avoid because you want to not repeat yourself as
specified WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
specified
via the dry principle.
Now quite simply, with respect to a function, all it does is it gives us a way
to reuse code
any nice easy manner.
In fact, to let you in in a little bit of a secret, we have already been using
some built
in functions already.
So let's take this example here.
If I type the keyword print and I just say, you know, my name is John.
If you actually thought about what's happening here, we simply pass in this
value and then
specify this keyword print and then suddenly when we run the actual script, the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
specify this keyword print and then suddenly when we run the actual script, the
text inside
happens to get printed out.
So what we're doing here, in fact, if I just hover over it, you can see in
brackets, this
is a function.
Now the reason why this is a function is because very, very often within our
Python code,
we want to be able to print something out.
This is something that happens a lot.
And so for the Python developers have made it a built in function, so you don't
have to
write the code in order to print things to your terminal.
You just reference the actual function name, which in this case here is simply WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
You just reference the actual function name, which in this case here is simply
called
print.
And then you pass in a particular value to that print function and the print
function is
going to handle all of the underlying details.
So we have these built in functions, but what we can also do is create our own
custom functions.
Now the way we do this is by utilizing the keyword def.
This is how we define a function and then we can give the function a name,
which is called
this Adder.
Okay.
Now what I will do here is within my parentheses, I'm going to specify two
particular arguments
that I'm going to accept into this function pretty much data I'm going to work WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that I'm going to accept into this function pretty much data I'm going to work
with.
I will say num1 as well as num2.
And then I'll have my colon and then indented within, we specify the logic that
we want to
use.
So inside the function, I will just create a variable called total number.
And I'll just make that equal to num1 plus num2.
And then all I will do is print out the total number.
So the cool thing is just like with the print function, we pass in some type of
statement,
you know, this is a test.
And this would simply be printed.
In our case here, what I will do is just simply call the Adder function and I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
In our case here, what I will do is just simply call the Adder function and I
'll pass
in the two numbers I want to add.
So I want to add, let's say the number two to the number seven.
So if I just save this here and I run example five dot p y and to enter, check
this out
right here.
We are actually adding the two values past in there because we have defined the
logic once,
as we can see right here, we can just easily reference that logic just by
giving the name
of the function.
So if I want to add more numbers such as, you know, one in three, save you,
then I don't
have to rewrite the same logic all over again.
I can just run the script, call the function to different times. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I can just run the script, call the function to different times.
And now I've got this nice piece of reusable code.
Now similarly, what we could do is create a custom function and we can do it to
check
the class of an IP address.
So what I will do is call it check class and I'm going to accept in an IP
address.
Now what I will do is inside this function, I will use some conditional logic.
I will see if the IP adder starts with, I'll say 10 dot, what I will do is just
print out.
This is a class A address and just follow the same logic we did before.
LIF, the IP adder starts with 172 dot, what I'll do is print. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
LIF, the IP adder starts with 172 dot, what I'll do is print.
This is a class B address.
Now what I could do is use an L statement here to print out the class C address
as we
did before, but this is not actually the best thing.
Let me show you why.
So this is a class C address.
So if I save this, let me just delete the adder one here and remove this.
We don't need that.
So if I just say check class and I pass in, let's go with the address of 10, 0,
0, 5.
Okay.
So let's run these scripts. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's run these scripts.
We see here this is a class A address.
If I want to run the function again and this time we'll test out or I don't
know let's
save 19 to 168 45 dot one.
Let's see what that one is.
Let's run this.
The first one is a class A address and it says here the second one is a class C
address.
Now this seems to be okay.
And similarly if we happen to pass in 172 161 dot one, save you run it again.
We can see the first one is class A, second one is class B. This appears to
work correctly.
Now just a caution about utilizing an L statement because it is a default WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now just a caution about utilizing an L statement because it is a default
statement and it's
going to execute absolutely every time if all of the prior conditions happen to
be false
and just be aware that that is the logic that you're using.
Let me give you an example here.
If I say check class, if I pass in the address and I go with 2001, DB8, ABCD,
colon colon
one, now this happens to be an IPv6 address.
Now check this out.
If I run this logic right here and enter, it says here that the last one is a
class C
address, even though it's actually not, it's an IPv6 address and this is just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
address, even though it's actually not, it's an IPv6 address and this is just
something
to watch out for.
So this part here is false as is this part here is false.
So this is always going to print out this particular statement, even though in
our case
here, this is not actually true.
So what we could do instead and I just want to highlight that you can do this
in your logic.
You don't have to end every F block with an L statement.
You can just use more LF statements so I can say F, IP adder starts with 190.
Then I can print out this as a class C address. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Then I can print out this as a class C address.
So if I add in a new address of 192168, 31.1 and I run this now, hit enter.
As we can see here, everything is being correctly classified.
Now just for posterity, what I can do is add an else clause that's a little bit
more appropriate
and this is more of a default.
Just say I'm not sure what the class is.
So that means if you do happen to add in, let's maybe say a 2001 DB8 colon
colon one
and IP 6 address, arrow up, hit enter.
We can see here the last one we have a way to handle any other data. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We can see here the last one we have a way to handle any other data.
Now crucially, look what happened.
In this particular function right here, we actually modified the logic a few
times.
But the cool thing is, is that we only had to modify it in one place.
And all we do is simply reference that one place to reuse the same logic over
and over
and over again.
So you can imagine if instead of actually using this particular function, we
wrote out
this entire logic for every single time you wanted to test out an IP address,
then similarly
every single time you wanted to update a logic such as adding in this new LF WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
every single time you wanted to update a logic such as adding in this new LF
statement
and this final else clause.
And we would have to rewrite that logic every single time we used it, whereas
instead if
we just create a function, we write the logic ones and then just can
continually reference
it.
So really this particular concept within Python, it is very, very common to use
all over
the place within network automation.
So when you happen to see that, we would keyword of death, just understand that
that's all
we're doing.
We're just creating a nice piece of reusable codes just to keep things neater,
more manageable, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
more manageable,
more portable and to make your lives a little bit easier.