CPython String Interning

While reading through CPython code for the last post, I came across string interning and how it’s used as a method of optimisation. What is string interning? String interning is a CPython optimisation that enables python to reuse immutable string objects instead of creating new ones. Much like the interned integers I wrote about in the last post, interened strings can be compared using pointer checks instead of an actual string comparision. ...

January 17, 2021 · 2 min

Small integer caching in python

I noticed something interesting about python a=10 b=10 print(a is b) True But also, a=1000 b=1000 print(a is b) False The small integer cache Python (specifically CPython) pre-caches integers in the range -5 to 256 at interpreter startup. These integers are interned, meaning there’s only one shared object for each of them. So when you do: a = 42 b = 42 Both a and b point to the same memory address, because 42 is inside the small-int cache. That’s why a is b is True. ...

December 17, 2020 · 2 min

Money doesn't float

Today I learned why float or double data types should not be used for storing currency values. TLDR Floats and doubles don’t accurately represent base 10 values that we use for money. Floats are approximated, and if used for representing values that need to be exact, arithmetic operations can give results that are slightly off. These errors can compound over time and become significantly large. Number Systems The story starts at number systems and the basic differences between the human way of counting numbers (Base 10) and the machine way of counting numbers (Base 2). ...

November 9, 2020 · 3 min