From 155e2e2f9afe134489ac762ecdffad39f3c7eab2 Mon Sep 17 00:00:00 2001 From: Luis Mazariegos <117244047+lmazariegos-c-chwy@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:11:19 -0400 Subject: [PATCH] Jun 14 --- .vscode/settings.json | 1 - Start/Ch 1/class_start.py | 28 +++++++++++++++++++++++++--- Start/Ch 1/definition_start.py | 9 +++++++-- Start/Ch 1/instance_start.py | 26 ++++++++++++++++++++------ Start/Ch 1/typecheck_start.py | 10 +++++++++- 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 32dee9a..e348c9a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,7 +17,6 @@ "files.autoSave": "afterDelay", "screencastMode.onlyKeyboardShortcuts": true, "terminal.integrated.fontSize": 18, - "workbench.activityBar.visible": true, "workbench.colorTheme": "Default Light Modern", "workbench.fontAliasing": "antialiased", "workbench.statusBar.visible": true, diff --git a/Start/Ch 1/class_start.py b/Start/Ch 1/class_start.py index 2c824f9..23d7e9e 100644 --- a/Start/Ch 1/class_start.py +++ b/Start/Ch 1/class_start.py @@ -4,26 +4,48 @@ class Book: # TODO: Properties defined at the class level are shared by all instances + BOOK_TYPES = ("HARDCOVER","PAPERBACK","EBOOK") # TODO: double-underscore properties are hidden from other classes + __booklist = None # TODO: create a class method + @classmethod + def get_book_types(cls): + return cls.BOOK_TYPES # TODO: create a static method + def getbooklist(): + if Book.__booklist == None: + Book.__booklist = [] + return Book.__booklist # instance methods receive a specific object instance as an argument # and operate on data specific to that object instance def set_title(self, newtitle): self.title = newtitle - def __init__(self, title): + def __init__(self, title, booktype): self.title = title + if (not booktype in Book.BOOK_TYPES): + raise ValueError(f"{booktype} is not a valid book type!") + else: + self.booktype = booktype + # def __repr__(self): + # return f"Book(title={self.title})" # TODO: access the class attribute - +print("Book Types: ", Book.get_book_types()) # TODO: Create some book instances - +b1 = (Book("Title1", "HARDCOVER")) +b2 = (Book("Title2", "PAPERBACK")) +# print(b1.title) +# print(b1.booktype) # TODO: Use the static method to access a singleton object +thebooks = Book.getbooklist() +thebooks.append(b1) +thebooks.append(b2) +print(thebooks) \ No newline at end of file diff --git a/Start/Ch 1/definition_start.py b/Start/Ch 1/definition_start.py index c519fd0..675b290 100644 --- a/Start/Ch 1/definition_start.py +++ b/Start/Ch 1/definition_start.py @@ -3,9 +3,14 @@ # TODO: create a basic class - +class Book: + def __init__(self, title): + self.title = title # TODO: create instances of the class - +book1 = Book("Brave New World") +book2 = Book("War and Peace") # TODO: print the class and property +print(book1) +print(book1.title) diff --git a/Start/Ch 1/instance_start.py b/Start/Ch 1/instance_start.py index 17df6f2..69e9066 100644 --- a/Start/Ch 1/instance_start.py +++ b/Start/Ch 1/instance_start.py @@ -5,21 +5,35 @@ class Book: # the "init" function is called when the instance is # created and ready to be initialized - def __init__(self, title): + def __init__(self, title, author, pages, price): self.title = title - # TODO: add properties + self.author = author + self.pages = pages + self.price = price + self.__secret = "This is a secret value." # TODO: create instance methods + def getprice(self): + if hasattr(self,"_discount"): + return self.price - (self.price * self._discount) + else: + return self.price + + def setdiscount(self, amount): + self._discount = amount # TODO: create some book instances -b1 = Book("War and Peace") -b2 = Book("The Catcher in the Rye") +b1 = Book("War and Peace","Leo Tolstoy",1225,39.95) +b2 = Book("The Catcher in the Rye","JD Salinger",234,29.95) # TODO: print the price of book1 - +print(b1.getprice()) # TODO: try setting the discount - +print(b2.getprice()) +b2.setdiscount(0.25) +print(b2.getprice()) # TODO: properties with double underscores are hidden by the interpreter +print(b2._Book__secret) \ No newline at end of file diff --git a/Start/Ch 1/typecheck_start.py b/Start/Ch 1/typecheck_start.py index 24555ab..4712010 100644 --- a/Start/Ch 1/typecheck_start.py +++ b/Start/Ch 1/typecheck_start.py @@ -19,9 +19,17 @@ def __init__(self, name): n2 = Newspaper("The New York Times") # TODO: use type() to inspect the object type +#print(type(b1)) +#print(type(n1)) # TODO: compare two types together - +#print(type(b1) == type(b2)) +#print(type(b1) == type(n2)) # TODO: use isinstance to compare a specific instance to a known type +print(isinstance(b1,Book)) +print(isinstance(n1,Newspaper)) +print(isinstance(n2,Book)) +print(isinstance(n2,object)) +