Ben Chuanlong Du's Blog

It is never too late to learn.

Dataclass vs namedtuple in Python

Tips and Traps

  1. Prefer Dataclass to namedtuple for many reasons.

    • A namedtuple is immutable while a dataclass can be both mutable (frozen=False which is the default) or immutable (frozen=True).

      However, namedtuple does have one advantage over dataclass. Members of a namedtuple is assible both via the dot operator and index. In situations where both dot accessing and index accessing of members is required, a namedtuple comes handy. For examples, a list of namedtuple objects can be used as the data for creating a pandas DataFrame but not a list of dataclass objects.

Collections in Java

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Oracel Java Tutorial on Collections

ArrayList

  1. You can point an element of Array or ArrayList to null, but remember that a null reference cannot invoke any method. For example, if you …

Collections in Kotlin

Fold vs Reduce

fold takes an initial value, and the first invocation of the lambda you pass to it will receive that initial value and the first element of the collection as parameters. reduce doesn't take an initial value, but instead starts with the first element of the collection as the accumulator (called sum in the following example).