Python | 内置函数(BIF)
阅读原文时间:2022年05月12日阅读:1

Python内置函数 | V3.9.1 | 共计155个


1.ArithmeticError

2.AssertionError

3.AttributeError

4.BaseException

5.BlockingIOError

6.BrokenPipeError

7.BufferError

8.BytesWarning

9.ChildProcessError

10.ConnectionAbortedError

11.ConnectionError

12.ConnectionRefusedError

13.ConnectionResetError

14.DeprecationWarning

15.EOFError

16.Ellipsis

17.EnvironmentError

18.Exception

19.False

20.FileExistsError

21.FileNotFoundError

22.FloatingPointError

23.FutureWarning

24.GeneratorExit

25.IOError

26.ImportError

27.ImportWarning

28.IndentationError

29.IndexError

30.InterruptedError

31.IsADirectoryError

32.KeyError

33.KeyboardInterrupt

34.LookupError

35.MemoryError

36.ModuleNotFoundError

37.NameError

38.None

39.NotADirectoryError

40.NotImplemented

41.NotImplementedError

42.OSError

43.OverflowError

44.PendingDeprecationWarning

45.PermissionError

46.ProcessLookupError

47.RecursionError

48.ReferenceError

49.ResourceWarning

50.RuntimeError

51.RuntimeWarning

52.StopAsyncIteration

53.StopIteration

54.SyntaxError

55.SyntaxWarning

56.SystemError

57.SystemExit

58.TabError

59.TimeoutError

60.True

61.TypeError

62.UnboundLocalError

63.UnicodeDecodeError

64.UnicodeEncodeError

65.UnicodeError

66.UnicodeTranslateError

67.UnicodeWarning

68.UserWarning

69.ValueError

70.Warning

71.WindowsError

72.ZeroDivisionError

73.__build_class__

74.__debug__

75.__doc__

76.__import__

77.__loader__

78.__name__

79.__package__

80.__spec__

81.abs

82.all

83.any

84.ascii

85.bin

86.bool

87.breakpoint

88.bytearray

89.bytes

90.callable

91.chr

92.classmethod

93.compile

94.complex

95.copyright

96.credits

97.delattr

98.dict

99.dir

100.divmod

101.enumerate

102.eval

103.exec

104.execfile

105.exit

106.filter

107.float

108.format

109.frozenset

110.getattr

111.globals

112.hasattr

113.hash

114help

115.hex

116.id

117.input

118.int

119.isinstance

120.issubclass

121.iter

122.len

123.license

124.list

125.locals

126.map

127.max

128.memoryview

129.min

130.next

131.object

132.oct

133.open

134.ord

135.pow

136.print

137.property

138.quit

139.range

140.repr

141.reversed

142.round

143.runfile

144.set

145.setattr

146.slice

147.sorted

148.staticmethod

149.str

150.sum

151.super

152.tuple

153.type

154.vars

155.zip


修饰符:类方法 @classmethod | 无需显式地传递类名做实参

class Computer:
    # 类属性modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    # 设定修饰符@类方法 | 类的函数或者叫类的方法output_modules
    @classmethod
    def output_modules(cls):
        for (i,s) in cls.__modules.items():
            print(i, ':', s)

# 调用类的方法output_modules,无需显式地传递类名做实参
Computer.output_modules()

#-------------------------------------------------------------
# 输出结果:
# cpu : Intel
# 内存 : 镁光
# 硬盘 : 970-Pro

也可被其他类直接进行调用(感觉有点全局的意思), 看例子代码如下:

class Computer:
    # 类属性modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    # 设定修饰符@类方法 | 类的函数或者叫类的方法output_modules
    @classmethod
    def output_modules(cls):
        for (i,s) in cls.__modules.items():
            print(i, ':', s)

class OtherClass:
    def __init__(self):
        pass
    def _test_OtherClass(self):
        # 调用类的方法output_modules,无需显式地传递类名做实参
        Computer.output_modules()

aaaa = OtherClass()
aaaa._test_OtherClass()

#-------------------------------------------------------------
# 输出结果:
# cpu : Intel
# 内存 : 镁光
# 硬盘 : 970-Pro

此修饰符可赋值给变量, 语法为:x = property(getx, setx, delx)

  • 如果是以此种方法的话, 函数名或者说是方法名可以不相同

如果是以装饰器形式使用的话, 函数名或者说是方法名必须相同, 例子代码如下:

class Computer:
    # 类属性 __modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    def __init__(self):
        pass

    # 获取字典的key
    @property
    def modules_property(self):
        # 字典 __modules的key 取出来变成列表
        __loops = [i for i in self.__modules]

        for ii in range(len(self.__modules)):
            print(__loops[ii], ':', self.__modules[__loops[ii]])

    # 给字典新增数据
    @modules_property.setter
    def modules_property(self, key_value):
        self.__modules[key_value[0]] = key_value[1]

    # 删除字典中内容, 这里没办法通过@modules_property.deleter以达到删除字典中某个键值
    # 所以换成了 静态方法 来删除键值
    @staticmethod
    def del_modules_property(__del, key):
        try:
            # dels.__modules.pop(key, 'Error, 删除的内容不存在!')
            __del.__modules.pop(key)
        except KeyError:
            print(f'Error, 删除的键: {key} 不存在!')# 这个引用变量 应该在v3.6版本以下不兼容...
            # print('Error, 删除的键: {keys} 不存在!'.format(keys=key))

# 实例化类
aaaa = Computer()

print('打印原有字典内容')
aaaa.modules_property
print('----------分隔符-----------')

print('打印新增后字典内容')
# 通过@modules_property.setter, 给字典新增数据
aaaa.modules_property = ('机箱', '海盗船')
# 通过@property,其实也是@getattr, 取出字典中的键值内容
aaaa.modules_property
print('----------分隔符-----------')

print('打印删除后字典内容')
# 通过静态方法@staticmethod, 删除字典中某个元素,或者说成删除字典中某个键值内容
Computer.del_modules_property(Computer, 'cpu')
# 通过@property, 再次打印字典内容,看下是否正常删除了
aaaa.modules_property

# -------------------------------------------------------------
# 打印原有字典内容
# cpu : Intel
# 内存 : 镁光
# 硬盘 : 970-Pro
# ----------分隔符-----------
# 打印新增后字典内容
# cpu : Intel
# 内存 : 镁光
# 硬盘 : 970-Pro
# 机箱 : 海盗船
# ----------分隔符-----------
# 打印删除后字典内容
# 内存 : 镁光
# 硬盘 : 970-Pro
# 机箱 : 海盗船

# 修饰符:静态方法 @staticmethod | 必须显式地传递类名做实参

class Computer:
    # 类属性modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    # 在静态方法search_module中定义形参var,准备传递类:Computer
    # 调用时必须显性地传递类名,才能实现类方法一样的效果
    # 设定修饰符@静态方法 | 类的函数或者叫类的方法search_module
    @staticmethod
    def search_module(var, module_value):
        print(var.__modules[module_value])

Computer.search_module(Computer, "cpu")
Computer.search_module(Computer, "内存")
Computer.search_module(Computer, "硬盘")

#-------------------------------------------------------------
# 输出结果:
# Intel
# 镁光
# 970-Pro

也可被其他类直接进行调用(有点全局的意思.....), 看例子代码如下:

class Computer:
    # 类属性modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    # 在静态方法search_module中定义形参var,准备传递类:Computer
    # 调用时必须显性地传递类名,才能实现类方法一样的效果
    # 设定修饰符@静态方法 | 类的函数或者叫类的方法search_module
    @staticmethod
    def search_module(var, module_value):
        print(var.__modules[module_value])

class OtherClass:
    def __init__(self):
        pass

    def _test_OtherClass(self):
        # 调用类的静态方法search_module,必须显式地传递类名做实参
        Computer.search_module(Computer, "cpu")
        Computer.search_module(Computer, "内存")
        Computer.search_module(Computer, "硬盘")

aaaa = OtherClass()
aaaa._test_OtherClass()

#-------------------------------------------------------------
# 输出结果:
# Intel
# 镁光
# 970-Pro

super函数不需要明确的给出任何 "被调用类" 的名称, 学习中觉得 子类-父类-超类 叫起来很绕, 就自认为叫成 "被调用类" 方便自己理解

  • 假设定义了三个类: A B C
  • 类A 继承 类B, 类A 是 类B 的子类 | 类B 是 类A 的父类(被调用类)
  • 类B 继承 类C, 类B 是 类C 的子类 | 类C 是 类B 的父类(被调用类)
  • 类A 间接继承 类C , 类C 是 类A 的超类(被调用类)
  • 例子待定

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章