Shahriar Tajbakhsh

Computer Sciencer in the making; professional Baseball player; hobbyist photo taker; best-selling author; occasional liar…

Read this first

Understanding write-through, write-around and write-back caching (with Python)

This post explains the three basic cache writing policies: write-through, write-around and write-back. Although caching is not a language-dependent thing, we’ll use basic Python code as a way of making the explanation and logic clearer and hopefully easier to understand. Running the Python code could also be helpful for simulating and playing around with different these caching policies.

A cache is a hardware or software component that stores data so future requests for that data can be served faster. Of course, this data is stored in a place where retrieval is faster than your actual backing store. For example, if you store a value in memory, it’ll usually be quicker to access than hitting the database and trying to read it from disk.

So, to start, let’s have two Python classes to represent our backing store and our cache. You can read from and write to each of them.

import time

Continue reading →

Underscores in Python

I’m in the process of starting something new: check out Highlight ( to watch and learn from the best tech and business talks on the planet.

This post discusses the use of the _ character in Python. Like with many things in Python, we’ll see that different usages of _ are mostly (not always!) a matter of convention.

 Single Lone Underscore (_)

This is typically used in 3 cases:

  1. In the interpreter: The _ name points to the result of the last executed statement in an interactive interpreter session. This was first done by the standard CPython interpreter, and others have followed too.

    >>> _
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name '_' is not defined
    >>> 42
    >>> _
    >>> 'alright!' if _ else ':('
    >>> _
  2. As a name: This is somewhat related to the previous point. _ is used as a throw-away name. This

Continue reading →

Python’s `NotImplemented` Type

This post discusses Python’s NotImplemented built-in constant/type; what it is, what it means and when it should be used.

 What is it?

>>> type(NotImplemented)
<type 'NotImplementedType'&gt

NotImplemented is one of Python’s six constants living in the built-in namespace. The others are False, True, None, Ellipsis and __debug__. Similar to Ellipsis, NotImplemented can be reassigned (shadowed). Assignments to it, even as an attribute name, do not raise a SyntaxError. So it isn’t really a “real/true” constant. Of course, we should never ever change it. But, for completeness:

>>> None = 'hello'
SyntaxError: can't assign to keyword
>>> NotImplemented
>>> NotImplemented = 'do not'
>>> NotImplemented
'do not'

 What does it mean and when should it be used?

NotImplemented is a special value which should be returned by the binary special methods (e.g. __eq__(), __lt__(), __a

Continue reading →

Importing `*` in Python

This post discusses Python’s from <module> import * and from <package> import *, how they behave and why it may be (is!) a bad idea to use them.

 Importing * from a module

from <module> import * means “I want access to all the names in <module> that I’m meant to have access to”. So, let’s say we have the following


public_variable = 42
_private_variable = 141

def public_function():
    print("I'm a public function! yay!")

def _private_function():
    print("Ain't nobody accessing me from another module...usually")

class PublicClass(object):

class _WeirdClass(object):

In the Python Interpreter, we can execute from something import * and see the following:

>>> from something import *
>>> public_variable
>>> _private_variable
NameError: name '_private_variable' is not defined
>>> public_function()
"I'm a public function! yay!"

Continue reading →

The Possibly Forgotten Optional `else` in Python’s `try` Statement

This post discusses the else clause in Python’s try statement. Although this particular use of else may not be as forgotten and controversial as its use in Python loops, a gentle reminder of how it works and when it’s useful may still be beneficial. Please note that this post only focuses on the else clause of the try statement and does not intend to cover the entire try statement. For a pretty good initial explanation of how the entire try statement works, refer to the official Python documentation on this topic.

A “normal” stripped down try statement seen regularly looks something like this:

    # some code that might raise exception
    # code to handle exception
    # code to run regardless of exception 

This is “normal” in the sense that to anyone coming from most other programming languages can understand/guess what it means:

  • Some code will be tried to

Continue reading →

The Forgotten Optional `else` in Python Loops

This post discusses Python’s for...else and while...else syntax, one of the most rarely used and misunderstood syntactic features in Python.

Both for and while loops in Python also take an optional else suite (like the if statement and the try statement do), which executes if the loop iteration completes normally. In other words, the else suite will be executed if we don’t exit the loop in any way other than its natural way. So, no break statements, no return statement, or no exceptions being raised inside the loop. Consider a simple (and useless) example:

>>> for i in range(5):
...     print(i)
... else:
...     print('Iterated over everything :)')
Iterated over everything :)

In the code above, we iterate over range(5) and print each number. Since we let the loop complete normally, the else suite is also executed and Iterated over everything :) is printed. Conversely

Continue reading →

Test Driven Development?


View →