#

Saturday, May 3, 2025

Working with Functions of Python for Network Engineers

Functions in a programming language are defined to get rid of repeating set of codes in a script. Following link is the official place to find built in functions in python.


We can also use built in function help() to get the info about a specific function through CLI, just by sending the name of the function we need help with in the parenthesis.

Basics of Defining and Calling Functions 

Following is the basic syntax to define a custom / user defined function in Python.






It can be called just by the name followed by parenthesis at anywhere in the code.
If passing variables to a function while calling it is necessary, following is the way to define it and call it.

Note:- "a" and "b" are parameters and "5" and "3" are called arguments.





The result will be;




Another example to push string arguments will be;









The result will be,




You can also notice that there is a doc string within triple quotation marks """XYZ""" which is the comment which help someone to understand the code and this is the text which optouts if someone send this function through the help function means if someone calls it by help(customf), following will be the result;







Note:- Some people prefer to view the doc string by the following way,

This is basically the same information as help funtion.


Following syntax specifies how we can define a default argument value in a function, which comes to play like in the following examples.










The result will automatically add "gmail.com" as the 2nd argument.




Following syntax shows how you can mix the order of sending arguments to a function called "keyword arguments", basically the Positional Arguments can not come after Keyword Arguments. "user" is a positional argument and domain is a keyword argument here.









Result will be,



You can send multiple arguments in a list to a function like the following.

Here the "num_list" parameter takes in the argument as a list and sum up all together, no matter how many items are in the list.






The above can be scripted in the following way also,

By this method, we don't want to send the argument as a List as the parameter is coded to take any amount of arguments. They are called star args (unpacking operator *) in python.

This can't be used with keyword arguments just like the above way but you can do something like the following if you use ** double stars.








This takes the passed arguments as a dictionary (key value pairs) and unpacking with iterating using for loop to make the following output.





We can send any amount of kwargs (keyword arguments) as you want like this.

We can also combine above different methods of sending arguments but have to use the following order.




Returning values from a Function

This is where the values which comes as a result of running a function can be used for some other thing later in a code. By default, a function returns none, but if we assigned the result to a variable, we can use it later like the following,











In the above example, "return email" means returning the final output of the function which is assigned to a variable called "email" is returned back to where the function is called and it is stored in the variable called "my_email".

So the result will be;




Note that you cannot call the variable "email" outside the function as it is cleared after running the function once.

Following example shows how to return multiple values from a funtion.











Scopes of Variables

In Python, there are 4 Scopes.

1. Built-in
2. Global
3. Local
4. Enclosing

Built-in variables are predefined keyword kind of variables which can be accessed from anywhere.
Global Scope is defined outside of any function so that it can be accessed from anywhere in the code.
Local is defined inside a function and can only be accessed within that function.
Enclosing is defined at parent functions which can be accessed by the child functions (functions within functions)

"gname" is a globally defined variable, "ename" is a enclosed variable and a local variable to function named brand() and "lname" is a locally defined variable inside the function nbrand() and nbrand() is a function inside brand().

If this script is run, the output will be like the following..

cisco
meraki
huawei
cisco

No comments:

Post a Comment