Last active
November 7, 2020 19:39
-
-
Save gidgid/efa43e75d71c38b18d07f62734d36b96 to your computer and use it in GitHub Desktop.
Properties are a great way to abstract over an internal structure
This file contains hidden or 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
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