Python for Network Engineers

Decorators With Parameters

By George El. • March 2019 • Reading time: 2 minutes

In this post we will continue on decorators, and I will show you how we can pass parameters to decorators. Please read the previous post Decorators in Python if you are not familiar with decorators. In the previous post we saw this example. def outerf(fn): from datetime import datetime def innerf(*args, **kwargs): print (datetime.

Decorators in Python

By George El. • March 2019 • Reading time: 2 minutes

In this post I will explain decorators in python. This is a rather advanced topic. You should first read about [First class functions and Closures] (/posts/python-advanced/first_class_functions_in_python_and_closures/), in previous post. As we saw in previous post, functions can accept functions as arguments and return functions. Lets see an example. def outerf(fn): from datetime import datetime def innerf(*args, **kwargs): print (datetime.

First Class Functions in Python and Closures

By George El. • March 2019 • Reading time: 2 minutes

In this post I will explain first class functions. According to wikipedia: “In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.

Generators and Generator Expressions

By George El. • March 2019 • Reading time: 3 minutes

In this post I will explain generators and generator expressions. Please read first the post on iterables and iterators. Generators are iterator objects that we can call to get the values one at a time. When a function has a yield statement, it returns a generator object. Then we can iterate through this, using a for loop or the next method.

Iterables and Iterators

By George El. • March 2019 • Reading time: 4 minutes

In this post I will explain the what is an iterable and what is the difference between Iterators and Iterables. An iterable is every object that can be iterated. In order for this to happen it must implement the __iter__ method, and the __iter__ method must return an iterator object. An iterator object is an object that implements the __next__ method.

True and False in Python

By George El. • March 2019 • Reading time: 2 minutes

In this Post I will describe what is True and False in Python. Then how to use “all” and “any” to combine these values and finally make a function that will check if an ip address is valid. In python all numbers are True except 0, every object that is not None, every string that is not empty, every list that is not empty, every dictionary that is not empty.

Local, Enclosing, Global, and Builtin Scope in Python

By George El. • March 2019 • Reading time: 2 minutes

In this post I will explain the difference between the various scopes in python. By scope we mean that each variable, depending on where it is defined, has a certain area where it is valid and as a concequence has also a certain lifetime. Lets start with an example a=1 def func(): a=2 print(a) func() print(a) We have two variables called “a”, one is defined outside of anything and we say it has a module or global scope and the other one is defined inside a function and it has a local scope.

Classes in Python

By George El. • March 2019 • Reading time: 6 minutes

In this post I will explain what classes are and then I will show you how to create instance methods and class methods and what is the difference between them. Then I will also show you how to create static methods. What are classes? Classes are blueprints that represent real life objects.

Lambda Expressions in Python

By George El. • March 2019 • Reading time: 3 minutes

In this post I will explain lambda expressions in python. Simply put, lambda expressions is another way to create a function in python. However this function has some restrictions. It does not have a name (that’s why they are also called anonymous), they can not have assignments they do not have a ‘return’ keyword and they can only be one line.

How to Use the Kwargs in Python

By George El. • March 2019 • Reading time: 3 minutes

In the previous post we saw how to unpack an iterable using the * operator and how to pass parameters to functions using the * operator. In this post I will show you how to pass parameters to functions using the ** operator and how to combine * and ** together.