Named tuples and dataclasses

A quick review of named tuples (standad and typed) and dataclasses.

From the Data Science from Scratch book.

namedtuple

Similar to dictionaries but

  • fields are named
  • values are immutable
In [1]:
from collections import namedtuple
import datetime
In [2]:
StockPrice = namedtuple('StockPrice', ['symbol', 'date', 'closing_price'])
In [3]:
type(StockPrice)
Out[3]:
type
In [4]:
price = StockPrice('MSFT', datetime.date(2018, 12, 14), 106.03)
price
Out[4]:
StockPrice(symbol='MSFT', date=datetime.date(2018, 12, 14), closing_price=106.03)
In [5]:
type(price)
Out[5]:
__main__.StockPrice
In [6]:
price.symbol, price.date, price.closing_price
Out[6]:
('MSFT', datetime.date(2018, 12, 14), 106.03)

NamedTuple

Simlar to namedtuple but

  • it is a class
  • fields have to be typed
  • as it is a class, it can have methods
In [7]:
from typing import NamedTuple
In [8]:
class StockPrice(NamedTuple):
    symbol: str
    date: datetime.date
    closing_price: float

    def is_high_tech(self) -> bool:
        return self.symbol in ['MSFT', 'GOOG', 'FB', 'AMZN', 'AAPL']
In [9]:
stockprice = StockPrice('MSFT', datetime.date(2019, 12, 14), 106.83)
stockprice
Out[9]:
StockPrice(symbol='MSFT', date=datetime.date(2019, 12, 14), closing_price=106.83)
In [10]:
stockprice.is_high_tech()
Out[10]:
True
In [11]:
StockPrice('TOOT', 12, 12).is_high_tech()
Out[11]:
False

Dataclasses

Similar to NamedTuple but

  • fields are mutable
In [12]:
from dataclasses import dataclass
In [13]:
@dataclass
class StockPrice2:
    symbol: str
    date: datetime.date
    closing_price: float

    def is_high_tech(self) -> bool:
        return self.symbol in ['MSFT', 'GOOG', 'FB', 'AMZN', 'AAPL']
In [14]:
stockprice2 = StockPrice2('MSFt', datetime.date(2018, 12, 14), 106.03)
stockprice2
Out[14]:
StockPrice2(symbol='MSFt', date=datetime.date(2018, 12, 14), closing_price=106.03)
In [15]:
stockprice2.closing_price /= 2
stockprice2.closing_price
Out[15]:
53.015

For NamedTuple this does not work

In [16]:
stockprice.closing_price /= 2
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-7ab00711d514> in <module>
----> 1 stockprice.closing_price /= 2

AttributeError: can't set attribute