python 學習彙總44:內建資料型別(入門基礎 tcy)
阿新 • • 發佈:2018-11-19
內建型別 2018/11/17
1.資料內建型別
None # 缺少值None表示無,# 是NoneType唯一值
NotImplemented # builtins.NotImplemented未實現
# 數值方法和比較方法未實現所提供運算元操作返回此值
# 用於對一些二元特殊方法(如:__eq__, __It__等)中作為返回值
number: # numbers.Number int, float, complex, bool(True,False) sequence: # typing.Sequence 不可變序列:str,unicode(python2), tuple, byte; 可變序列:list;bytearray;array;range
map:dict
set: set, frozenset
2.程式結構內建型別 可呼叫型別(函式,方法例項類) types.BuiltinFunctionType # 內建函式或方法 type # 內建型別和類 object types.FunctionType # 使用者定義函式 types.MethodType # 類方法 模組 types.ModeleType # 模組 類 object # 所有型別和類的祖先 型別 type # 內建型別和類的型別
3.直譯器內部使用內建型別 types.Codetype # 位元組編譯程式碼 types.FrameType # 執行幀 types.GeneratorType # 生成器物件 types.TracebackType # 異常棧跟蹤 slice # 由擴充套件切片生成 Ellipsis # 和...是等價;用在擴充套件切片迴圈是EllipsisType型別常量 __debug__ #是一個bool型別的常量
4.其他型別
參考types.typing,inspect,builtins等模組。
2.example:
例項1:
... == Ellipsis # True
例項2:
a = [1, 2, 3, 4]
a.append(a)
a # [1, 2, 3, 4, [...]]
len(a) # 5
a[4] # [1, 2, 3, 4, [...]]
a[4][4] # [1, 2, 3, 4, [...]]
例項3:
class Example(object):
def __getitem__(self,index):
print(index)
e=Example()
e # __main__.Example object at 0x0000000002AFE128>
e[3,...,4] # (3, Ellipsis, 4)
例項4:
class A(object):
def __init__(self, name, value):
self.name = name
self.value = value
def __eq__(self, other):
print('self:', self.name, self.value)
print('other:', other.name, other.value)
return self.value == other.value # 判斷兩個物件的value值是否相等
a1 = A('Tom', 1)
a2 = A('Jim', 1)
print(a1 == a2)
# 輸出
# self: Tom 1
# other: Jim 1
# True
例項5:
class A(object):
def __init__(self, name, value):
self.name = name
self.value = value
def __eq__(self, other):
print('self:', self.name, self.value)
print('other:', other.name, other.value)
return NotImplemented
a1 = A('Tom', 1)
a2 = A('Jim', 1)
print(a1 == a2)
# 輸出
# self: Tom 1
# other: Jim 1
# self: Jim 1
# other: Tom 1
# False