Last active
May 16, 2020 19:30
-
-
Save raeq/f9ae235f219e11b54128cb2b34d859d6 to your computer and use it in GitHub Desktop.
Versioned Class
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
""" | |
A versioned class example. | |
""" | |
__version__: str = "0.1" | |
class Book: | |
""" | |
A sinple book class. | |
""" | |
counter: int = 0 | |
__class_version__: str = __version__ | |
def __init__(self, isbn: str, title: str, author: str): | |
""" | |
The constructor | |
""" | |
self.isbn = isbn | |
self.title = title | |
self.author = author | |
self.page = 0 | |
self.notes = [] | |
type(self).counter += 1 | |
self.sequencenumber: int = type(self).counter | |
@property | |
def version(self): | |
""" | |
Returns the Class version, not an object's version. | |
""" | |
return type(self).__class_version__ | |
def set_note(self, note: str, page: int)-> int: | |
""" | |
Sets a reader note. | |
""" | |
self.notes.append((note, page)) | |
def goto_page(self, page_number: int): | |
""" | |
Sets the current page number. Maximum of 1000. | |
""" | |
if page_number < 1000 > 0: | |
self.page = page_number | |
def __repr__(self) -> str: | |
return (f"Object id: {id(self)}. Version: {self.version}, " | |
f"ObjSeq: {self.sequencenumber}, Total created: {Book.counter}, " | |
f"Author: {self.author}, " | |
f"Title: {self.title}. ISBN: {self.isbn}.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment