Python面向?qū)ο缶幊讨惖倪\算
1、運算概念的理解
運算(Operation
)是操作邏輯的抽象
- 運算體現(xiàn)一種操作邏輯,在廣義角度來說任何程序都是一種運算
Python
解釋器通過保留方法預(yù)留了一批運算的接口,需要重載- 保留方法一般對應(yīng)運算符,
Python
中運算體現(xiàn)為運算符的重載 - 運算本質(zhì)上體現(xiàn)了交互關(guān)系、包含關(guān)系和常規(guī)的操作關(guān)系
運算重載的限制
- 不能重載
Python
語言內(nèi)置類型的運算符 - 不能新建運算符,只能通過重載完成
is
,and
,not
,or
不能被重載
2、運算符的重載
2.1 算術(shù)運算符
一元運算符:+
、-
、~
二元運算符:+
、-
、*
、/
、//
、%
、divmod()
、 pow()
、**
、<<
、>>
、&
、^
、|
保留方法 | 對應(yīng)操作 | 描述 |
---|---|---|
.__neg__(self) | -obj | 定義對象取負的運算邏輯 |
.__pos__(self) | +obj | 定義對象取正的運算邏輯 |
.__abs__(self) | abs(obj) | 定義對象絕對值的運算邏輯 |
.__invert__(self) | ~obj | 定義對象取反的運算邏輯 |
.__add__(self, other) | obj + other | 定義了兩個對象加法的運算邏輯 |
.__sub__(self, other) | obj - other | 定義了兩個對象減法的運算邏輯 |
.__mul__(self, other) | obj * other | 定義了兩個對象乘法的運算邏輯 |
.__truediv__(self, other) | obj / other | 定義了兩個對象除法的運算邏輯 |
.__floordiv__(self, other) | obj // other | 定義了兩個對象整數(shù)除的運算邏輯 |
.__mod__(self, other) | obj % other | 定義了兩個對象模的運算邏輯 |
.__divmod__(self, other) | divmod(obj, other) | 定義了兩個對象除模的運算邏輯 |
.__pow__(self, other) | obj ** other | 定義對象冪的運算邏輯 |
.__lshift__(self, other) | obj << other | 定義對象左移的運算邏輯 |
.__rshift__(self, other) | obj >> other | 定義對象右移的運算邏輯 |
.__and__(self, other) | obj & other | 定義兩個對象位于運算邏輯 |
.__xor__(self, other) | obj ^ other | 定義兩個對象位異或的運算邏輯 |
.__or__(self, other) | `obj | other` |
2.2 比較運算符
比較運算符:<、<=、==、!=、>、>=
保留方法 | 對應(yīng)操作 |
---|---|
.__lt__(self, other) | obj < other |
.__le__(self, other) | obj <= other |
.__eq__(self, other) | obj == other |
.__be__(self, other) | obj != other |
.__gt__(self, other) | obj > other |
.__ge__(self, other) | obj >= other |
兩個對象比較操作的運算重載
2.3 成員運算
成員獲?。?/strong>[]
、def
、 .eversed()
成員判斷:in
保留方法 | 對應(yīng)操作 | 描述 |
---|---|---|
.__getitem__(self, key) | obj[k] | 定義獲取對象中序號K元素的運算邏輯,K為整數(shù) |
.__setitem__(self, key, v) | obj[k] = v | 定義賦值對象中序號K元素的運算邏輯 |
.__delitem__(self, key) | del obj[k] | 定義刪除對象中序號K元素的運算邏輯 |
.__reversed__(self) | obj.reversed() | 定義對象逆序的運算邏輯 |
.__contains__(self, item) | item in obj | 定義in操作符對應(yīng)的運算邏輯 |
2.4 其他運算
Python
內(nèi)置函數(shù):rep(),str(),len(),int(),flaot,complex(),round(),bytes(),bool(),format(),.format
(常用方法)
保留方法 | 對應(yīng)操作 | 描述 |
---|---|---|
__repr__(self) | repr(obj) | 定義對象可打印字符串的運算邏輯 |
__str__(self) | str(obj) | 定義對象字符串轉(zhuǎn)換的運算邏輯 |
__len__(self) | len(obj) | 定義對象長度操作的運算邏輯 |
__int__(self) | int(obj) | 定義對象整數(shù)轉(zhuǎn)換的運算邏輯 |
__float__(self) | float(obj) | 定義對象浮點數(shù)轉(zhuǎn)換的運算邏輯 |
__complex__(self) | complex(obj) | 定義對象復數(shù)轉(zhuǎn)換的運算邏輯 |
__round__(self) | round(obj) | 定義對象四舍五入的運算邏輯 |
__bytes__(self) | bytes(obj) | 定義對象字節(jié)串轉(zhuǎn)換的運算邏輯 |
__bool__(self) | bool(obj) | 定義對象布爾運算的運算邏輯 |
.__format__(self, format_spec) | obj.format() format(obj) | 定義對象格式化輸出的運算邏輯 |
3、Python類的多態(tài)
多態(tài) _(Polymorphism
)_是針對方法,體現(xiàn)方法靈活性的多態(tài);簡單的說,他包含兩部分
參數(shù)類型的多態(tài):一個方法能夠處理多個類型的能力
Python
的函數(shù)/方法沒有類型聲明限制,天然支持參數(shù)類型的多態(tài)性
Python
編程理念在于:文檔約束,而非語法約束
對不同參數(shù)類型的區(qū)分以及功能,需要有程序員完成
參數(shù)形式的多態(tài):一個方法能夠接受多個參數(shù)的能力
Python
的函數(shù)/方法可以支持可變參數(shù),支持參數(shù)形式的多態(tài)性
Python
的類方法也是函數(shù),函數(shù)的各種定義方式均有效
對不同參數(shù)個數(shù)以及默認值的確定,需要由程序員完成
多態(tài)是OOP的一個傳統(tǒng)概念,Python天然支持多態(tài),不需要特殊語法,示例代碼如下所示:
import abc class Animal(metaclass=abc.ABCMeta): #同一類事物:動物 @abc.abstractmethod def talk(self): pass class Cat(Animal): #動物的形態(tài)之一:貓 def talk(self): print('say miaomiao') class Dog(Animal): #動物的形態(tài)之二:狗 def talk(self): print('say wangwang') class Pig(Animal): #動物的形態(tài)之三:豬 def talk(self): print('say aoao')
到此這篇關(guān)于Python
面向?qū)ο缶幊讨惖倪\算的文章就介紹到這了,更多相關(guān)Python類的運算內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。