内置类型 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