Created
November 14, 2023 15:46
-
-
Save bbelderbos/57d18c4714a2ee473513c088727f4064 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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", "[email protected]") | |
# 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", "[email protected]") | |
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", "[email protected]") | |
process_data(person) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment