Skip to content
Open
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
18 changes: 13 additions & 5 deletions Start/Ch 1/class_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@

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

# TODO: create a class method

@classmethod
def get_book_types(cls):
return cls.BOOK_TYPES
# TODO: create a static method

# 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
if(not booktype in Book.BOOK_TYPES):
raise ValueError(f"{booktype} is not a valid book type")
else:
self.booktype= booktype


def __init__(self, title):
def __init__(self, title, booktype):
self.title = title


# TODO: access the class attribute

print("Book types: ", Book.get_book_types())

# TODO: Create some book instances


b1= Book("Title1", "HARDCOVER")
b2= Book("Title1", "COMIC")
# TODO: Use the static method to access a singleton object