Decorators is one of the most used features in python. In this blog we will discuss,

  • decorators
  • how python executes decorators
  • various ways of defining decorators
  • singledispatch which is a decorator used for function overloading defined in the python standard library.

In this blog, we will use the example of baking a pizza to understand the concepts mentioned above.

Decorator Basics

A decorator is a callable which works as a wrapper, taking a function as an argument and perform some processing with the decorated function.

Sample output

Just adding more cheese here...
Just baking pizza here...
And Done!

In the…


Decorators are widely used in Python and an extensively written topic. In this blog we will see what is decorator design pattern, when to use use it and how to code it.

To learn about basics of Python decorators, refer to the following article

By definition, decorator pattern attaches additional responsibilities to an object dynamically. In other words decorator pattern creates wrappers over an object such that object gets additional functionality defined by the wrappers.

Let’s take an example to understand decorator pattern. Suppose we have to develop an app that lets users create stories in different social media apps…


By definition observer pattern defines, one to many dependencies between the objects so that if one object changes state all of its dependencies are notified and updated automatically. In other words observer pattern defines publisher and subscriber model, in which any change in publisher gets notified to the subscribers. Observer patten is used widely in developing mobile apps.

This blog uses the observer pattern to solve team Avengers’ problem. Nick Fury is looking to develop a tool to notify all the Avengers in case of emergency. …


The strategy pattern is a type of behavioral design pattern. In strategy pattern, we define a family of algorithms, encapsulate each one of them, and make them interchangeable. Strategy lets algorithms vary independently from the client that uses them.

To demonstrate the strategy pattern, let’s take an example of car types. In the UML diagram below class Car is the base class which is inherited by classes Mazda, Tesla, Audi and BMW. Class Car defines the following 3 functions:

  • drive() — defines the implementation of drive functionality of car
  • cruiseControl() — defines implementation of cruise control of car
  • moonRoof() —…


Mughal’s have ruled South Asia for some two centuries and have had an enormous impact on the Indian subcontinent. One such example is Taj Mahal. In this blog we will use the Mughal empire’s family tree to understand topological sort in graph theory.

In graph theory, topological sorting is one of the most interesting topics. This blog will cover theory of topological sort, implementation of topological sort in python, problems that are solved by topological sort and fun facts about the Mughal empire.

Topological sort which is also known as topological ordering in directed acyclic graph (DAG), is linear ordering…


If you code in python then I am sure you have come across local, nonlocal and global variables. In this blog, I have explained scope of variables with examples in python.

LOCAL

Variables defined as function parameters are local to the function. In other words, they have a local scope and are available only within the function in which they are declared.

Sample output

Hi!, I am Batman. Nice to meet you!
Traceback (most recent call last):
File "local_variable.py", line 6, in <module>
print(f"Outside variable_scope function, superhero = {superhero}")
NameError: name 'superhero' is not defined

In the function variable_scope, variable…


This is the second and final part of the series. You can find the first part of the blog here. The first part covered basic python multi-threading. The second part discusses more advanced topics of multi-threading.

Python multi-threading is very similar to Dwight Schrute. If implemented properly it will be your good friend. If not, then we all know what happened in the snowball fight!

DEADLOCK

Deadlock occurs when thread is waiting for a resource. Let’s take an example of the episode “Murder” from the office when Michael, Andy, Dwight and Pam find themselves in a fake Mexican handoff.

Let’s…


I have always been a huge fan of concurrent programming and famous sitcom television series The Office. In this blog I am trying to relate multi-threading in Python with different episodes of The office.

THREADS

In sequential programming, like a program (process) to print “Hello World!”, the program has a start, a set of sequence of instructions to execute and an end.

Just like sequential programming, a thread has a start, a set of sequence of instructions to execute and an end. But threads cannot coexist without a process. In other words, threads are a subset of a process. …

Ankush Kumar Singh

Curious programmer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store