資訊之芽 2023 Python 語法班
Author: Sean 韋詠祥
speaker_name = 'Sean Wei'
speaker_birthday = 'June'
speaker_skills = ['Cybersecurity', 'Diving']
speaker1_name = 'Sean Wei'
speaker1_birthday = 'June'
speaker1_skills = ['Cybersecurity', 'Diving']
speaker2_name = 'William Mou'
speaker2_birthday = 'October'
speaker2_skills = ['Photography', 'Supercomputing']
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
跟函式定義的方式很像
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']
看起來似乎沒有比較簡潔
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'])
method
方法:class 裡面的 functioninstance
實體:依照 class 做出來的物件attribute
屬性:class 裡面的變數self
__init__()
obj = classname()
建構時會被呼叫幫 class Speaker
實作 learn()
函式
讓講師向另一位講師學習相關技能
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'])
speaker1.learn(speaker2)
Inheritance
分別定義 Student 及 Employee
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}!')
核心概念:DRY(Don’t Repeat Yourself)
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
super()
super()
執行上層函式class NormalClass:
pass
class NormalClass():
pass
class DerivedClass(BaseClass):
pass
class DerivedClass(moduleName.BaseClass):
pass
class DerivedClass(Base1, Base2, Base3):
pass
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()
__mro__
Method Resolution Order
print(Student.__mro__)
print(Third.__mro__)
print(Test.__mro__)
class-level vs instance-level
class Phone:
pwd = '0000'
foo = Phone()
bar = Phone()
print(f'{Phone.pwd=}, {foo.pwd=}, {bar.pwd=}')
Output: Phone.pwd='0000', foo.pwd='0000', bar.pwd='0000'
foo.pwd = '1234'
print(f'{Phone.pwd=}, {foo.pwd=}, {bar.pwd=}')
Output: Phone.pwd='0000', foo.pwd='1234', bar.pwd='0000'
Phone.pwd = '5678'
print(f'{Phone.pwd=}, {foo.pwd=}, {bar.pwd=}')
Output: Phone.pwd='5678', foo.pwd='1234', bar.pwd='5678'
可以用 vars()
函式
print(vars(Phone)) # {'__module__': '__main__',
# 'pwd': '5678', '__dict__': ....}
print(vars(foo)) # {'pwd': '1234'}
print(vars(bar)) # {}
或是透過 __dict__
取得
# Same as above
print(Phone.__dict__)
print(foo.__dict__)
print(bar.__dict__)
Phone.color = 'blue'
print(f'{Phone.color=}, {foo.color=}, {bar.color=}')
Output: Phone.color='blue', foo.color='blue', bar.color='blue'
foo.price = 34_900
print(f'{Phone.price=}, {foo.price=}, {bar.price=}')
Output: AttributeError
為什麼我的 list 不乖
class Person:
skills = []
# Note here
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=}')
Magic methods
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 __str__(self):
return 'price and calorie is xxx' # 小練習
# 記得先重新執行 xxx = Food(x, x) 語句
print(french_fries)
# price = 65 NTD, calorie = 260 kcal
class Food:
# ...
def __add__(self, other):
price = 0 # 請完成
calorie = 0 # 請完成
mixture = Food(price, calorie)
return mixture
# 記得先重新執行 xxx = Food(x, x) 語句
oreo_nuggets = oreo_flurry + chicken_nuggets
print(oreo_nuggets)
# price = 120 NTD, calorie = 620 kcal
把 class Food
實作得更完整
class Food:
# ...
def __mul__(self, num):
pass # 計算 num 份食物的價格、熱量
def __truediv__(self, num):
pass # 平分給 num 個人後每份長怎樣
def __eq__(self, other):
pass # 比較「💲價格」相不相同,暫時無視熱量
def __gt__(self, other):
pass # 比較「🔥熱量」是否更高,暫時無視價格
# (optional) 針對「💲價格」,除了 eq 相等之外,實作 ne 不相等
# (optional) 針對「🔥熱量」,除了 gt 外,實作 lt 小於、ge、le
@Decorator
@dataclass