Skip to content

Instantly share code, notes, and snippets.

@gidgid
Last active November 7, 2020 19:39
Show Gist options
  • Save gidgid/efa43e75d71c38b18d07f62734d36b96 to your computer and use it in GitHub Desktop.
Save gidgid/efa43e75d71c38b18d07f62734d36b96 to your computer and use it in GitHub Desktop.
Properties are a great way to abstract over an internal structure
from typing import List, Set
import attr
@attr.s(auto_attribs=True)
class Employee:
name: str
age: int
hobbies: List[dict] # 1
@property
def hobbies_names(self) -> Set[str]: # 2
return {hobby["name"] for hobby in self.hobbies}
def test_hobbies_names_abstracts_the_hobbies_internal_structure():
emp = Employee(
name="John",
age=42,
hobbies=[
{"_id": "1234", "name": "Rock Climbing"},
{"_id": "5678", "name": "Acrobatics"},
],
)
assert emp.hobbies_names == {"Rock Climbing", "Acrobatics"} # 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment