python 3+ nice features


There are 3 ways to deal with strings in python.

  • use of %-formatting (old school)
  • use of str.format()
  • use of f-strings
my_var = "hello"

print("my var is %s" %my_var)

print("my var is {}".format(my_var))

print(f"my var is {my_var}")

I’m planning on having a blog just on f-strings

another nice feature of the new f-strings that allow to quicky print variable name and values

>>> def my_method():
       return "Hello world"
>>> print(f"{my_method() = }")
my_method()= "Hello world"


>>> print('iterable'.center(50, '-'))



When dealing with file paths, instead of using os (os.path …), python 3.4 offers pathlib. The big advantage of pathlib is that it seems easier/shorter to write the same code.

using os

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

using pathlib

from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent


Type hinting

This is a nice tool that can help during debugging process and help the readability of a code. Passing or returning the wrong type won’t raise an error this is why this is more to help understand the code.

def is_this_word_in_that_sentence(word: str, sentence, str) -> bool:
   return word in sentence

is_this_word_in_that_sentence("plane", "I love flying")
# False



I used to wonder what enumerations are really good for (back in my Matlab time) and then switch to python and started missing it. As usual you don’t realize the chance you have of having something until you loose it. But thanks to python 3.4+, this is back.

from enum import Enum, auto

class Planes(Enum):
   CORSAIR = auto()
   B17 = auto()
   CESSNA = auto()

# Planes.B17


lru cache

Data classes