Skip to content

Instantly share code, notes, and snippets.

@highsmallxu
Last active May 19, 2020 21:33
Show Gist options
  • Save highsmallxu/611cef8f11f31f6e88b8427d420a46c8 to your computer and use it in GitHub Desktop.
Save highsmallxu/611cef8f11f31f6e88b8427d420a46c8 to your computer and use it in GitHub Desktop.
from typing import NamedTuple
import collections
from dataclasses import dataclass
# regular class
class TransactionObject:
def __init__(self, sender, receiver, date, amount):
self.sender = sender
self.receiver = receiver
self.date = date
self.amount = amount
# named tuple from collections
TransactionNamedTuple = collections.namedtuple('TransactionNamedTuple',['sender','receiver','date', 'amount'])
# named tuple from typing class
class TransactionNamedTupleTyping(NamedTuple):
sender: str
receiver: str
date: str
amount: float
# dataclass
@dataclass
class TransactionDataClass:
sender: str
receiver: str
date: str
amount: float
# dataclass with slot
@dataclass
class TransactionDataClassWithSlot:
__slots__ = ['sender', 'amount', 'receiver', 'date']
sender: str
receiver: str
date: str
amount: float
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment