Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Main#11

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
lpmazariegos wants to merge1 commit intoLinkedInLearning:main
base:main
Choose a base branch
Loading
fromlpmazariegos:main
Open
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsStart/Ch 1/class_start.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsStart/Ch 1/definition_start.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsStart/Ch 1/instance_start.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionStart/Ch 1/typecheck_start.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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))


[8]ページ先頭

©2009-2025 Movatter.jp