[課後作業] 第042講:魔法方法:算術運算|課後測試題及答案
《零基礎入門學習Python》視頻下載地址:傳送門
*}XK_(~測試題:n.版權屬於:bbs.fishc.com EzunG2ql+=0.自Python2.2以後,對類和類型進行了統一,做法就是將int()、float()、str()、list()、tuple()這些BIF轉換為工廠函數。請問所謂的工廠函數,其實是什麼原理?N }Xrc
Ow`?Yms
fXq=C
1.當實例對象進行加法操作時,會自動調用什麼魔法方法?{|y}^SH Kg
2;dE*
z"qZ>-j
2.下邊代碼有問題嗎?(運行起來似乎沒出錯的說^_^)Wf)M`J!"5G8E#)y*
- class Foo:
- def foo(self):
- self.foo = "I love FishC.com!"
- return self.foo
- >>> foo = Foo()
- >>> foo.foo()
- 'I love FishC.com!'
複製代碼
5T%<g^3.寫出下列算術運算符對應的魔法方法:Z%y*F><xz-~a|w
運算符
|
對應的魔法方法
|
+
|
|
-
|
|
*
|
|
/
|
|
//
|
|
%
|
|
divmod(a, b)
|
|
**
|
|
<<
|
|
>>
|
|
&
|
|
^
|
|
|
|
|
sLJ`iqRh~4.以下代碼說明Python支持什麼風格?>yT#]t{@D b來自:bbs.fishc.com KF
- def calc(a, b, c):
- return (a + b) * c
- >>> a = calc(1, 2, 3)
- >>> b = calc([1, 2, 3], [4, 5, 6], 2)
- >>> c = calc('love', 'FishC', 3)
- >>> print(a)
- 9
- >>> print(b)
- [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
- >>> print(c)
- loveFishCloveFishCloveFishC
複製代碼
-(#^P動動手:2Ne>mfE70.我們都知道在Python中,兩個字符串相加會自動拼接字符串,但遺憾的是兩個字符串相減卻拋出異常。因此,現在我們要求定義一個Nstr類,支持字符串的相減操作:A – B,從A中去除所有B的子字符串。@~`-Q{5,BJY{ 6R示例:DW #vz
- >>> a = Nstr('I love FishC.com!iiiiiiii')
- >>> b = Nstr('i')
- >>> a - b
- 'I love FshC.com!'
複製代碼
9(來自:bbs.fishc.com $*1.移位操作符是應用於二進制操作數的,現在需要你定義一個新的類Nstr,也支持移位操作符的運算:j}*.,P
- >>> a = Nstr('I love FishC.com!')
- >>> a << 3
- 'ove FishC.com!I l'
- >>> a >> 3
- 'om!I love FishC.c'
複製代碼
MDZ*}5If"A2.定義一個類Nstr,當該類的實例對象間發生的加、減、乘、除運算時,將該對象的所有字符串的ASCII碼之和進行計算:^=* J?:OC"cv6P!m
- >>> a = Nstr('FishC')
- >>> b = Nstr('love')
- >>> a + b
- 899
- >>> a - b
- 23
- >>> a * b
- 201918
- >>> a / b
- 1.052511415525114
- >>> a // b
- 1
複製代碼
3.請寫下這一節課你學習到的內容:格式不限,回憶並複述是加強記憶的好方式!2C&zPowered by bbs.fishc.comPowered by bbs.fishc.com A~!wujxk
回复您的答案即可查看參考答案!)版權屬於:bbs.fishc.com Wxt(uNVy%Qw=Y來自:bbs.fishc.com_-m<3*ydX8測試題答案:qo;&9L`{
本帖隱藏的內容
$K{OEo50.自Python2.2以後,對類和類型進行了統一,做法就是將int()、float()、str()、list()、tuple()這些BIF轉換為工廠函數。請問所謂的工廠函數,其實是什麼原理?Lb09`sD答:工廠函數,其實就是一個類對象。當你調用他們的時候,事實上就是創建一個相應的實例對象。m{Powered by bbs.fishc.com &c,#><V-f|2F:mH)
- # a 和b 是工廠函數(類對象) int 的實例對象
- >>> a = int('123')
- >>> b = int('345')
- >>> a + b
- 468
複製代碼
q#`8*xw1.當實例對象進行加法操作時,會自動調用什麼魔法方法?OJ|HETVlK0)j答:對象a和b相加時(a + b),Python會自動根據對象a的__add__魔法方法進行加法操作。`|2b}BA"L+cR}paS2.下邊代碼有問題嗎?(運行起來似乎沒出錯的說^_^)CFhR(-來自:bbs.fishc.com IaPb< y?U;1 "d
- class Foo:
- def foo(self):
- self.foo = "I love FishC.com!"
- return self.foo
- >>> foo = Foo()
- >>> foo.foo()
- 'I love FishC.com!'
複製代碼
答:這絕對是一個溫柔的陷阱,這種BUG比較難以排查,所以一定要注意:類的屬性名和方法名絕對不能相同!如果代碼這麼寫,就會有一個難以排查的BUG出現了:b6W>h:sFola:]d$i0
- class Foo:
- def __init__(self):
- self.foo = "I love FishC.com!"
- def foo(self):
- return self.foo
- >>> foo = Foo()
- >>> foo.foo()
- Traceback (most recent call last):
- File "<pyshell#21>", line 1, in <module>
- foo.foo()
- TypeError: 'str' object is not callable
複製代碼
ME`QC*83.寫出下列算術運算符對應的魔法方法:BQh lMXn:1版權屬於:bbs.fishc.com xCR&IE
運算符
|
對應的魔法方法
|
+
|
__add__(self, other)
|
-
|
__sub__(self, other)
|
*
|
__mul__(self, other)
|
/
|
__truediv__(self, other)
|
//
|
__floordiv__(self, other)
|
%
|
__mod__(self, other)
|
divmod(a, b)
|
__divmod__(a, b)
|
**
|
__pow__(self, other[, modulo])
|
<<
|
__lshift__(self, other)
|
>>
|
__rshift__(self, other)
|
&
|
__and__(self, other)
|
^
|
__xor__(self, other)
|
|
|
__or__(self, other)
|
b;XF0s1@hPowered by bbs.fishc.com4.以下代碼說明Python支持什麼風格?9lPowered by bbs.fishc.com 3Okb_K3@0]qB:
- def calc(a, b, c):
- return (a + b) * c
- >>> a = calc(1, 2, 3)
- >>> b = calc([1, 2, 3], [4, 5, 6], 2)
- >>> c = calc('love', 'FishC', 3)
- >>> print(a)
- 9
- >>> print(b)
- [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
- >>> print(c)
- loveFishCloveFishCloveFishC
複製代碼
答:說明Python支持鴨子類型(duck typing)風格。A>2I3iJ?BRjw1_3詳見:【擴展閱讀】鴨子類型(duck typing)X?St-uA"K
kIK-#N8T1"%v@_-K>版權屬於:bbs.fishc.com b動動手答案:
本帖隱藏的內容
F2lRpkHq0.我們都知道在Python中,兩個字符串相加會自動拼接字符串,但遺憾的是兩個字符串相減卻拋出異常。因此,現在我們要求定義一個Nstr類,支持字符串的相減操作:A – B,從A中去除所有B的子字符串。mt&*來自:bbs.fishc.com Wn<Q示例:.|}$>
- >>> a = Nstr('I love FishC.com!iiiiiiii')
- >>> b = Nstr('i')
- >>> a - b
- 'I love FshC.com!'
複製代碼
答:只需要重載__sub__魔法方法即可。G@k3qoq%z6H
- class Nstr(str):
- def __sub__(self, other):
- return self.replace(other, '')
複製代碼
來自:bbs.fishc.com ;ZS%D:F1.移位操作符是應用於二進制操作數的,現在需要你定義一個新的類Nstr,也支持移位操作符的運算:};=WN3u8n #'.g?H
- >>> a = Nstr('I love FishC.com!')
- >>> a << 3
- 'ove FishC.com!I l'
- >>> a >> 3
- 'om!I love FishC.c'
複製代碼
答:只需要重載__lshift__和__rshift__魔法方法即可。U;<Lfr^#cPuw):
- class Nstr(str):
- def __lshift__(self, other):
- return self[other:] + self[:other]
- def __rshift__(self, other):
- return self[-other:] + self[:-other]
複製代碼
N8ihmO=2.定義一個類Nstr,當該類的實例對象間發生的加、減、乘、除運算時,將該對象的所有字符串的ASCII碼之和進行計算:Grus`gw.L4NY%
- >>> a = Nstr('FishC')
- >>> b = Nstr('love')
- >>> a + b
- 899
- >>> a - b
- 23
- >>> a * b
- 201918
- >>> a / b
- 1.052511415525114
- >>> a // b
- 1
複製代碼
代碼清單:u$TOVh?Z
- class Nstr:
- def __init__(self, arg=''):
- if isinstance(arg, str):
- self.total = 0
- for each in arg:
- self.total += ord(each)
- else:
- print("參數錯誤!")
- def __add__(self, other):
- return self.total + other.total
- def __sub__(self, other):
- return self.total - other.total
- def __mul__(self, other):
- return self.total * other.total
- def __truediv__(self, other):
- return self.total / other.total
- def __floordiv__(self, other):
- return self.total // other.total
複製代碼
當然,你還可以這樣做::04Powered by bbs.fishc.com h~R-
- class Nstr(int):
- def __new__(cls, arg=0):
- if isinstance(arg, str):
- total = 0
- for each in arg:
- total += ord(each)
- arg = total
- return int.__new__(cls, arg)
複製代碼
3.請寫下這一節課你學習到的內容:格式不限,回憶並複述是加強記憶的好方式!小甲魚希望你認真對待作業就像你希望小甲魚推出高質量視頻一樣渴望^_^
在Python2.2之前,類和類型是分開的,在Python2.2之後,作者試圖對這兩個東西進行統一,做法就是將int()、float()、str()、list()、tuple( )這些BIF轉換為工廠函數,那什麼是工廠函數呢?
我們試圖演示一下:
-
-
<class 'builtin_function_or_method'>
len()是一個普通的內置函數,返回類似也告訴了我們這一點,但是
-
-
-
-
然後我們發現:
-
-
-
-
-
所以,工廠函數就是類對象。
Python的魔法方法還提供了讓你自定義對象的數值處理,通過對我們這些魔法方法進行重寫,你可以自定義任何對象間的算術運算。
__add__(self, other)
|
定義加法的行為:+
|
__sub__(self, other)
|
定義減法的行為:-
|
__mul__(self, other)
|
定義乘法的行為:*
|
__truediv__(self, other)
|
定義真除法的行為:/
|
__floordiv__(self, other)
|
定義整數除法的行為://
|
__mod__(self, other)
|
定義取模算法的行為:%
|
__ divmod __(self, other)
|
定義當被 divmod () 調用時的行為,divmod(a, b)返回一個元組:(a//b, a%b)
|
__pow__(self, other[, modulo])
|
定義當被power() 調用或** 運算時的行為
|
__ lshift __(self, other)
|
定義按位左移位的行為:<<
|
__rshift__(self, other)
|
定義按位右移位的行為:>>
|
__and__(self, other)
|
定義按位與操作的行為:&
|
__xor__(self, other)
|
定義按位異或操作的行為:^
|
__or__(self, other)
|
定義按位或操作的行為:|
|
例如:
-
-
def __add__(self, other):
-
return int.__sub__(self, other)
-
def __sub__(self, other):
-
return int.__add__(self, other)
-
-
-
-
-
-
-
-
發現了沒,我在這裡對原有int 的加法和減法進行了改寫,把加法變為減法,減法變為加法。
0 留言:
發佈留言