Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Part 1: Types, Objects & Memory
PYTHON

Part 1: Types, Objects & Memory

Revise Python immutability, object identity, interning, is versus ==, division rules, and the Python memory model.

1. Everything is an Object

  • Every value is an object; variables are names bound to objects (reference semantics), not boxes.
  • id(x) + is test identity; == tests equality (calls __eq__).
  • Assignment a = b aliases — both names point to the same object.
a = [1, 2]
b = a            # same list object
b.append(3)
print(a)         # [1, 2, 3] — b is not a copy

2. Immutable vs Mutable

ImmutableMutable
int, float, bool, str, bytes, tuple, frozensetlist, dict, set, custom objects
HashableNot hashable (list/dict/set)
Safe as dict keysmust not be dict keys
  • Tuple with a list inside is still “unhashable” in spirit — immutable reference but mutable inner object. t = ([1],); t[0][0]=9 works.

Interview trap: tuple is immutable as a container, but a hashing its contents failing is a classic — hash((1, [2]))TypeError.

3. Interning & small-int caching

  • Small integers -5..256 are pre-created, singletonsa = 257; b = 257; a is b is implementation-defined, usually False.
  • CPython constant folding may merge ints in the same code unit.
  • String interning for simple identifiers can make "hello" is "hello" True.
  • Never rely on is for numbers/strings — use ==.

4. Operators & the truthiness trap

  • Truthiness (not strict boolean): 0, 0.0, "", [], {}, set(), None are falsy; everything else truthy.
  • bool(x) calls __len__ for containers, otherwise __bool__.
  • / vs //: / gives float; // floor-divides (ints → int). -7 // 2 = -4 (floor, not truncation!). % sign follows the divisor.
  • == chaining: 1 < x < 5 is chained comparison — Python expands it to 1 < x and x < 5.
  • is vs == again: objects equal by content (==) need not be identical (is).
5 / 2      # 2.5
5 // 2     # 2
-7 // 2    # -4  (floor, not truncation)
-7 % 3     # 2   (sign follows divisor)
1 < 3 < 5  # True (chained)

5. The memory/object model pytricks

  • copy vs deepcopy: copy.copy shallow — new outer container, shared contents; copy.deepcopy — full copy recursively.
  • Identical immutable values can share memory freely (safe), mutable values never automatically share copy semantics.
  • sys.getrefcount(x) shows reference count — never manage it manually.
  • None is a singleton — the proper way to compare to None is is None, not == None.

6. The classic intern questions

  1. == vs is — equality of value vs identity of object. [] == [] True, [] is [] False.
  2. a is None — the canonical None check.
  3. Mutable defaultdef f(x=[]) is evaluated once → shared default across calls (see Part 2).
  4. String/bytes confusionstr is immutable Unicode; bytes immutable byte sequence.
  5. Integer division/ vs // semantics, including negatives and floor behaviour.

My Private Notes

Notes are auto-saved locally to this device.