Skip to content

Instantly share code, notes, and snippets.

@iaindillingham
Created September 10, 2021 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iaindillingham/76943437ce41676621665e8b92d66a95 to your computer and use it in GitHub Desktop.
Save iaindillingham/76943437ce41676621665e8b92d66a95 to your computer and use it in GitHub Desktop.
Data Classes vs Named Tuples
import dataclasses
import random
import typing
@dataclasses.dataclass
class PointDC:
x: float
y: float
@profile
def make_points_dc():
return [PointDC(random.random(), random.random()) for x in range(1_000_000)]
class PointNT(typing.NamedTuple):
x: float
y: float
@profile
def make_points_nt():
return [PointNT(random.random(), random.random()) for x in range(1_000_000)]
if __name__ == "__main__":
make_points_dc()
make_points_nt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment