intermediate pythonista

by Obi Ike-Nwosu

Problem solver by birth. Software engineer and Computer scientist by training.

Read this first

Intermezzo: A little History

 The Evolution of Python

In December 1989, Guido Van Rossum started work on a language that he christened Python. Guido Van Rossum had been part of the team that worked on the ABC programming language as part of the Amoeba operating systems in the 1980s at CWI (Centrum Wiskunde & Informatica) in Amsterdam and although he liked the ABC programming language, he was frustrated by a number of features or lack of thereof. Guido wanted a high level programming language that would speed up the development of utilities for the the Amoeba project and ABC was not the answer. The ABC programming language would however play a very influential role in the development of python as Guido took parts he liked from the language and provided solutions for aspects of the ABC programming language that he found frustrating.

Guido published the first version of the Python programing language in February

Continue reading →


Classes and Objects III: Metaclasses, Abstract Base Classes and Class Decorators

 Metaclasses

Everything in python is an object including classes; if a class is an object then such class must have another class from which it is created.
Consider, an instance, f, of a user defined class Foo; we can find out the type/class of the instance, f by using the inbuilt method, type and in this case it seen that the type of f is Foo.


>>> class Foo(object):
...     pass
... 
>>> f = Foo()
>>> type(f)
<class '__main__.Foo'>
>>> 

Given that everything in python is an object including classes, we can also introspect on a class object to find out the type/class for such class.
To illustrate this, we introspect on our previous class, Foo, using the type inbuilt method.


class Foo(object):
    pass

>>> type(Foo)
<class 'type'>

In new style classes such as that defined above, the class used for creating all other class objects is the type class. This applies to user defined

Continue reading →


Classes and Objects I

In python, everything is an object. Classes provide the mechanism for creating new kinds of objects. In this tutorial, we ignore the basics of classes and object oriented programming and focus on topics that provide a better understanding of object oriented programming in python. It is assumed that we are dealing with new style classes. These are python classes that inherit from object super class.

 Defining Classes

The class statement is used to define new classes. The class statement defines a set of attributes, variables and methods, that are associated with and shared by a collection of instances of such a class. A simple class definition is given below:


    class Account(object):
        num_accounts = 0

        def __init__(self, name, balance):
            self.name = name 
            self.balance = balance 
            Account.num_accounts += 1

        def

Continue reading →


Classes and Objects II: Descriptors

Descriptors are an esoteric but integral part of the python programming language. They are used widely in the core of the python language and a good grasp of descriptors provides a python programmer with an extra trick in his or her toolbox. To set the stage for the discussion of descriptors I describe some scenarios that a programmer may encounter in his or her daily programming activities; I then go ahead to explain what descriptors are and how they provide elegant solutions to these scenarios. For this writeup, I refer to python version using new style classes.

  1. Consider a program in which we need to enforce strict type checking for object attributes. Python is a dynamic languages and thus does not support type checking but this does not prevent us from implementing our own version of type checking regardless of how rudimentary it may be. The conventional way to go about type

Continue reading →


The Function II: Python Function Decorators

Function decorators enable the addition of new functionality to a function without altering the function’s original functionality. Prior to reading this post, it is important that you have read and understood the first installment on python functions. The major take away from that tutorial is that python functions are first class objects; a result of this is that:

  1. Python functions can be passed as arguments to other functions.
  2. Python functions can be returned from other function calls.
  3. Python functions can be defined inside other functions resulting in closures.

The above listed properties of python functions provide the foundation needed to explain function decorators. Put simply, function decorators are “wrappers” that let you execute code before and after the function they decorate without modifying the function itself. The structure of this tutorial follows an excellent stack

Continue reading →


The Function

Python functions are either named or anonymous set of statements or expressions. In python, functions are first class objects. This means that there is no restriction on the usage of functions. Python functions can be used just like any other python value such as strings and numbers. Python functions have attributes that can be introspected on using the inbuilt python dir function as shown below:

def square(x):
    return x**2

>>> square
<function square at 0x031AA230>
>>> dir(square)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc',

Continue reading →


Introduction to Python Generators

Generators are a very fascinating concept in python; generators have a wide range of applications from simple lazy evaluation to mind-blowing advanced concurrent execution of tasks (see David Beazley). Before we dive into the fascinating world of python generators, we take a little detour to explain python iterators, a concept that I feel is integral to grasping generators.

 Python Iterators

Simply put, an iterator in python is any python type that can be used with a for loop. Python lists, tuples, dicts and sets are all examples of inbuilt iterators. One may ask, what about these types that make them iterators and is this a property of only inbuilt types?
These types are iterators because they implement the iterator protocol. Then again, What is the iterator protocol?
To answer this question requires another little detour. In python, there are some special object methods commonly

Continue reading →


Python Comprehensions

Python comprehensions are syntactic constructs that enable sequences to be built from other sequences in a clear and concise manner. Python comprehensions are of three types namely:

  1. list comprehensions,
  2. set comprehensions and
  3. dict comprehensions.

List comprehension constructs have been part of python since python 2.0 while set and dict comprehensions have been part of python since python 2.7.

 List Comprehensions

List comprehensions are by far the most popular python comprehension construct. List comprehensions provide a concise way to create new list of elements that satisfy a given condition from an iterable. An iterable is any python construct that can be looped over. Examples of inbuilt iterables include lists, sets and tuples. The example below from the python documentation illustrates the usage of list comprehensions. In this example, we want to create a list of squares of

Continue reading →


Intermediate Pythonista table of contents

 Table of Contents

  • Python comprehensions
  • Introduction to Python Generators
  • The Function
  • The Function II: Python Function Decorators
  • Classes and Objects
  • Classes and Objects II: Descriptors *Classes and Objects III: Types and Metaclasses
  • Intermezzo I: A little Python History
  • A Short Note on Multiple Inheritance in Python
  • Python Modules and Packages

 What is this all about?

I have been working with python for close to five years and in all these years it has been a struggle to find tutorials or blogs that covered a predefined set of intermediate python programming language topics. Most of the tutorials are geared towards beginners or just cover a single advanced topic. I have therefore decided to make my own set of tutorials for python topics that I consider to be of intermediate level of difficulty.
If you have any feedback or suggestions, don’t hesitate to reach out to me on

Continue reading →

no