Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Main #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 25 additions & 3 deletions Start/Ch 1/class_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 7 additions & 2 deletions Start/Ch 1/definition_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 20 additions & 6 deletions Start/Ch 1/instance_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 9 additions & 1 deletion Start/Ch 1/typecheck_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))