Polyglot CheatSheet - Data Class

Updated: 2018-06-26

Simple classes that are used for storing data.

Python

namedtuple

namedtuple is immutable

>>> Node = namedtuple('Node', 'val left right')
>>> Node = namedtuple('Node', ['val','left','right'])

>>> Node.__new__.__defaults__ = 0, None, None
>>> Node()
Node(val=0, left=None, right=None)

dataclass

dataclass is added in 3.7

@dataclass decorator and they are supposed to be "mutable namedtuples with default"

Scala

Scala provides case class:

case class Field(
    name: String,
    count: Option[Long]
)