資訊之芽 2023 Python 語法班
Author: Sean 韋詠祥
Slide: https://hackmd.io/@Sean64/py-class
逐個複製太麻煩了,整理出來方便使用
speaker1_name = 'Sean Wei'
speaker1_birthday = 'June'
speaker1_skills = ['Cybersecurity', 'Diving']
speaker2_name = 'William Mou'
speaker2_birthday = 'October'
speaker2_skills = ['Photography', 'Supercomputing']
speaker3_name = 'Sirius Koan'
speaker3_birthday = 'July'
speaker3_skills = ['Softball', 'Mail']
speaker1 = {
'name': 'Sean Wei',
'birthday': 'June',
'skills': ['Cybersecurity', 'Diving']
}
speaker2 = {
'name': 'William Mou',
'birthday': 'October',
'skills': ['Photography', 'Supercomputing']
}
speaker3 = {
'name': 'Sirius Koan',
'birthday': 'July',
'skills': ['Softball', 'Mail']
}
class Speaker:
pass
speaker1.name = 'Sean Wei'
speaker1.birthday = 'June'
speaker1.skills = ['Cybersecurity', 'Diving']
speaker2.name = 'William Mou'
speaker2.birthday = 'October'
speaker2.skills = ['Photography', 'Supercomputing']
speaker3.name = 'Sirius Koan'
speaker3.birthday = 'July'
speaker3.skills = ['Softball', 'Mail']
class Speaker:
def __init__(self, name, birthday, skills):
self.name = name
self.birthday = birthday
self.skills = skills
speaker1 = Speaker('Sean Wei', 'June',
['Cybersecurity', 'Diving'])
speaker2 = Speaker('William Mou', 'October',
['Photography', 'Supercomputing'])
speaker3 = Speaker('Sirius Koan', 'July',
['Softball', 'Mail'])
class Speaker:
def __init__(self, name, birthday, skills):
self.name = name
self.birthday = birthday
self.skills = skills
def learn(self, teacher):
print(f'{self=}, {self.skills=}')
print(f'{teacher=}, {teacher.skills=}')
# Write your code here
speaker1 = Speaker('Sean Wei', 'June', ['Cybersecurity', 'Diving'])
speaker2 = Speaker('William Mou', 'October', ['Photography', 'Supercomputing'])
speaker3 = Speaker('Sirius Koan', 'July', ['Softball', 'Mail'])
speaker1.learn(speaker2)
class Student:
def __init__(self, name, age, student_id):
self.name = name
self.age = age
self.student_id = student_id
def say_hi():
print(f'Hello, I am {name}!')
class Employee:
def __init__(self, name, age, employee_id):
self.name = name
self.age = age
self.employee_id = employee_id
def say_hi():
print(f'Hello, I am {name}!')
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hi():
print(f'Hello, I am {name}!')
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
class Employee(Person):
def __init__(self, name, age, employee_id):
super().__init__(name, age)
self.employee_id = employee_id
class First:
def __init__(self):
print('Initializing first class')
# super().__init__() # no need
class Second(First):
def __init__(self):
print('Initializing second class')
super().__init__()
class Third(Second):
def __init__(self):
print('Initializing third class')
super().__init__()
foo = Third()
class Foo:
def __init__(self):
print('Initializing foo class')
super().__init__() # note here
class Bar:
def __init__(self):
print('Initializing bar class')
# super().__init__() # no need
class Test(Foo, Bar):
def __init__(self):
print('Initializing test class')
super().__init__()
baz = Test()
class Phone:
pwd = '0000'
foo = Phone()
bar = Phone()
print(f'{Phone.pwd=}, {foo.pwd=}, {bar.pwd=}')
class Person:
skills = []
def learn(self, skill):
self.skills.append(skill)
sean = Person()
sean.learn('Cybersecurity')
sean.learn('Diving')
print(f'{sean.skills=}')
mou = Person()
mou.learn('Photography')
mou.learn('Supercomputing')
print(f'{mou.skills=}')
class Person:
def __init__(self):
# creates a new empty list for each person
self.skills = []
def learn(self, skill):
self.skills.append(skill)
sean = Person()
sean.learn('Cybersecurity')
sean.learn('Diving')
print(f'{sean.skills=}')
mou = Person()
mou.learn('Photography')
mou.learn('Supercomputing')
print(f'{mou.skills=}')
class Food:
def __init__(self, price, calorie):
self.price = price # Unit: NTD
self.calorie = calorie # Unit: kcal
chicken_nuggets = Food(65, 260)
french_fries = Food(65, 530)
oreo_flurry = Food(55, 360)
corn_soup = Food(40, 90)
class Food:
def __init__(self, price, calorie):
self.price = price # Unit: NTD
self.calorie = calorie # Unit: kcal
def __str__(self):
return 'price and calorie is xxx' # 小練習
def __add__(self, other):
price = 0 # 請完成
calorie = 0 # 請完成
mixture = Food(price, calorie)
return mixture
chicken_nuggets = Food(65, 260)
french_fries = Food(65, 530)
oreo_flurry = Food(55, 360)
corn_soup = Food(40, 90)
oreo_nuggets = oreo_flurry + chicken_nuggets
print(oreo_nuggets)
# price = 120 NTD, calorie = 620 kcal
class Food:
# ...
def __mul__(self, num):
pass # 計算 num 份食物的價格、熱量
def __truediv__(self, num):
pass # 平分給 num 個人後每份長怎樣
def __eq__(self, other):
pass # 比較「💲價格」是否相同,暫時無視熱量
def __ne__(self, other):
pass # (optional) 比較「💲價格」是否不同
def __gt__(self, other):
pass # 比較「🔥熱量」是否更高
def __lt__(self, other):
pass # (optional) 比較「🔥熱量」是否更低
def __ge__(self, other):
pass # (optional) 比較「🔥熱量」是否更高或相同
def __le__(self, other):
pass # (optional) 比較「🔥熱量」是否更低或相同
chicken_nuggets = Food(65, 260)
french_fries = Food(65, 530)
oreo_flurry = Food(55, 360)
corn_soup = Food(40, 90)
print(oreo_flurry * 5) # 五杯冰炫風多少錢
print(chicken_nuggets / 6) # 每塊麥克雞塊熱量多高
print(french_fries + corn_soup > oreo_flurry) # 薯條 + 玉米濃湯 > 冰炫風
結束!
感謝各位 owo//
BTW, SITCON 夏令營準備要開始報名了
https://www.instagram.com/p/Crs2yM3hlrW/
歡迎揪校內有興趣的同學抱團參加
回去有空可以看看上一屆的影片
Learn More →
資訊之芽 2023 Python 語法班
Author: Sean 韋詠祥
Slide: https://hackmd.io/@Sean64/py-class
Sean 非官方 閒聊簡介
Jun 3, 2025公路監理資料、司法院 裁判書、政府電子採購網、監察院 政治獻金、公職人員 財產申報、台電 圖號座標、電桿坐標及桿號、離岸風場區塊、路口監視器 即時影像、高速公路 交通資料庫、TDX 運輸資料流通服務
Feb 12, 2025從標案查出文具包的廠商「鴻祈廣告」,幾年前也標過中華郵政的案子、無法履約被起訴求償 / 撰文者:259T Sean Wei
Oct 23, 2024工程三館 B1 EC015 門口 EC015 後方視角 EC016 門口 EC016 後方視角 EC022 講師視角(整修前) EC022 後方視角(整修前) 工程三館 1F EC114 後方視角(格局與 EC115 相同) EC114 講師視角(格局與 EC115 相同) EC122 講師視角 EC122 後方視角 工程三館 3F EC316 電腦教室 講師視角 EC316 電腦教室 後方視角 EC345 研討室 門口 EC345 研討室 內部 工程三館 4F EC411 EC412 研討室 門口 EC411 研討室 EC412 研討室 EC427 研討室 門口 交大工程三館平面圖 工程三館地下室平面圖 工程三館一樓平面圖 工程三館三樓平面圖 工程三館四樓平面圖 工程四館 B1 工程四館 B1 電梯 EDB07 EDB08 門口 EDB07 後方視角 EDB26 門口 EDB26 講師視角 EDB26 後方視角 EDB27 門口 EDB27 後方視角 工程四館 1F ED101 講師視角 ED101 後方視角 ED102 後方視角 ED102 前方視角 ED103 後方視角 ED103 前方視角 ED116 門口 ED117 門口 ED117 後方視角 工程四館 正門門口 工程四館 大廳 正拍 工程四館 大廳 斜拍 工程四館 大廳 沙發區 工三工四 連接道
Jul 10, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up