Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created November 14, 2023 15:46
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 bbelderbos/57d18c4714a2ee473513c088727f4064 to your computer and use it in GitHub Desktop.
Save bbelderbos/57d18c4714a2ee473513c088727f4064 to your computer and use it in GitHub Desktop.
# initial code
def process_data(name, age, address, phone, email):
print(f"Processing data for {name}, {age}, living at {address}. Contact info: {phone}, {email}")
process_data("Alice", 30, "123 Main St", "555-1234", "alice@example.com")
# refactored using dataclass
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
address: str
phone: str
email: str
def process_data(person: Person):
print(f"Processing data for {person.name}, {person.age}, living at {person.address}. Contact info: {person.phone}, {person.email}")
person = Person("Alice", 30, "123 Main St", "555-1234", "alice@example.com")
process_data(person)
# refactored using namedtuple
from typing import NamedTuple
class Person(NamedTuple):
name: str
age: int
address: str
phone: str
email: str
def process_data(person: Person):
print(f"Processing data for {person.name}, {person.age}, living at {person.address}. Contact info: {person.phone}, {person.email}")
person = Person("Alice", 30, "123 Main St", "555-1234", "alice@example.com")
process_data(person)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment