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

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
    >>> _
    42
    >>> 'alright!' if _ else ':('
    'alright!'
    >>> _
    'alright!'
    
  2. As a name: This is somewhat related to the previous point. _ is used as a throw-away name. This will allow the next person reading your code to know that, by convention, a certain name is assigned but not intended to be used. For instance, you may not be...

Continue reading →


Python’s `NotImplemented` Type

This post has now moved to my Medium page.

In other news, if you’re interested in a curated list of must-watch Python videos, check out my py-must-watch list on Highlight.

View →


Importing `*` in Python

This post has now moved to my Medium page.

In other news, if you’re interested in a curated list of must-watch Python videos, check out my py-must-watch list on Highlight.

View →


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

This post has now moved to my Medium page.

In other news, if you’re interested in a curated list of must-watch Python videos, check out my py-must-watch list on Highlight.

View →


The Forgotten Optional `else` in Python Loops

This post has now moved to my Medium page.

In other news, if you’re interested in a curated list of must-watch Python videos, check out my py-must-watch list on Highlight.

View →


Test Driven Development?

photo1.jpg

View →