Created
March 6, 2022 03:48
-
-
Save YuzuRyo61/b500759c3fff6e21c3600c5a16b12fb3 to your computer and use it in GitHub Desktop.
Python classmethod practice
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
from typing import Optional | |
from pprint import pp | |
class Actor: | |
__id: int | |
name: Optional[str] = None | |
description: Optional[str] = None | |
@property | |
def actor_id(self): | |
return self.__id | |
def __init__( | |
self, | |
target_id: int, | |
name: Optional[str] = None, | |
description: Optional[str] = None | |
): | |
self.__id = target_id | |
self.name = name | |
self.description = description | |
@classmethod | |
def fetch(cls, target_id: int): | |
# fetch data | |
return cls(target_id, | |
name='Actor name', | |
description='Actor description') | |
@classmethod | |
def fetch_many(cls, *target_ids: int): | |
# fetch data | |
data = [] | |
for tid in target_ids: | |
data.append(cls.fetch(tid)) | |
return data | |
if __name__ == "__main__": | |
a1 = Actor.fetch(1) | |
print(a1.actor_id) | |
a2 = Actor.fetch_many(1, 2, 3) | |
pp(a2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment