Python3 内建函数总结(二)
发布时间:2017-11-24T14:52:42:手机请访问
Python help() 函数
help() 函数用于查看函数或模块用途的详细说明。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
>>>help('sys') # 查看 sys 模块的帮助 ……显示帮助信息…… >>>help('str') # 查看 str 数据类型的帮助 ……显示帮助信息…… >>>a = [1,2,3] >>>help(a) # 查看列表 list 帮助信息 ……显示帮助信息…… >>>help(a.append) # 显示list的append方法的帮助 ……显示帮助信息…… In [2]: import sys In [3]: help(sys) """ Help on built-in module sys: NAME sys MODULE REFERENCE https://docs.python.org/3.6/library/sys The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above. DESCRIPTION """ <strong> <blockquote>hex() 函数用于将10进制整数转换成16进制整数。</blockquote> [code lang="python"] In [5]: bin(10) Out[5]: '0b1010' In [6]: bin(1100) Out[6]: '0b10001001100' |
Python id() 函数
id() 函数用于获取对象的内存地址。
1 2 3 4 5 6 7 |
In [8]: id(a) Out[8]: 4556247704 In [9]: b = "ooo" In [10]: id(b) Out[10]: 4557691400 |
Python isinstance() 函数
isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
isinstance() 与 type() 区别:
type() 不会认为子类是一种父类类型,不考虑继承关系。
isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
1 2 3 4 5 6 7 8 |
In [12]: a Out[12]: '168seo' In [13]: isinstance(a,str) Out[13]: True In [14]: isinstance(a,int) Out[14]: False |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
In [15]: class A: ...: pass ...: In [16]: class B(A): ...: pass ...: In [17]: isinstance(B(),A) Out[17]: True In [18]: type(B()) == A() Out[18]: False In [20]: type(B()) == B Out[20]: True |

Python issubclass() 函数
issubclass() 方法用于判断参数 class 是否是类型参数 classinfo 的子类。
1 2 3 4 5 6 7 8 9 10 11 |
In [15]: class A: ...: pass ...: In [16]: class B(A): ...: pass ...: Out[21]: True |
Python iter() 函数
iter() 函数用来生成迭代器。
1 2 3 4 5 6 7 |
In [22]: b = range(10) In [23]: b Out[23]: range(0, 10) In [24]: iter(b) Out[24]: <range_iterator at 0x10f8bd900> |
Python locals() 函数
locals() 函数会以字典类型返回当前位置的全部局部变量。
对于函数, 方法, lambda 函式, 类, 以及实现了 __call__ 方法的类实例, 它都返回 True。
1 2 3 4 5 6 7 8 9 10 |
>>> locals() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>} >>> >>> def run(arg): z = 1 print(locals()) >>> run(100) {'z': 1, 'arg': 100} |
Python next() 函数
next() 返回迭代器的下一个项目
next 语法:
next(iterator[, default])
参数说明:
iterator -- 可迭代对象
default -- 可选,用于设置在没有下一个元素时返回该默认值,如果不设置,又没有下一个元素则会触发 StopIteration 异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/usr/bin/python # -*- coding: UTF-8 -*- # 首先获得Iterator对象: it = iter([1, 2, 3, 4, 5]) # 循环: while True: try: # 获得下一个值: x = next(it) print(x) except StopIteration: # 遇到StopIteration就退出循环 break |
返回结果:

Python3 reversed 函数
reversed 函数返回一个反转的迭代器。
reversed 执行的本质是 __reversed__
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#!/usr/bin/env python3 # 字符串 seqString = 'Runoob' print(list(reversed(seqString))) # 元组 seqTuple = ('R', 'u', 'n', 'o', 'o', 'b') print(list(reversed(seqTuple))) # range seqRange = range(5, 9) print(list(reversed(seqRange))) # 列表 seqList = [1, 2, 4, 3, 5] print(list(reversed(seqList))) |
返回的结果是:

Python setattr() 函数
setattr 函数对应函数 getatt(),用于设置属性值,该属性必须存在。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
In [31]: class A: ...: a = "68seo.cn" In [32]: aa = A() In [33]: getattr(aa,a) In [34]: getattr(aa,'a') Out[34]: '68seo.cn' In [36]: getattr(aa,'a') Out[36]: 999 |
Python staticmethod() 函数
python staticmethod 返回函数的静态方法。
该方法不强制要求传递参数,如下声明一个静态方法:
Python3 静态方法调用实例
[code language=""]
class Demo(object):
@staticmethod
def print_demo():
print("静态方法")
def main():
print("不用实例化直接调用静态方法")
Demo.print_demo()
print("实例化以后也可以调用静态方法")
dd = Demo()
dd.print_demo()
print("*"*10)
if __name__ == '__main__':
main()
➜ Python3 入门和进阶 python3 Untitled-1.py
# 不用实例化直接调用静态方法
# 静态方法
# 实例化以后也可以调用静态方法
# 静态方法
# **********
Python super() 函数
super() 函数用于调用下一个父类(超类)并返回该父类实例的方法。
super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。
MRO 就是类的方法解析顺序表, 其实也就是继承父类方法时的顺序表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class D1(object): def __init__(self): self.parent = "I am a parent " print("in the parent") def bar(self, message): print("{} from message".format(message)) class D2(D1): def __init__(self, *args, **kwargs): super().__init__() print("child") def bar(self, messsage): super().bar(messsage) print("child bar function") print(self.parent) def main(): d2 = D2() d2.bar('thi is a message') if __name__ == '__main__': main() # ➜ d2 python3 c4.py # in the parent # child # thi is a message from message # child bar function # I am a parent |
Python __import__() 函数
__import__() 函数用于动态加载类和函数 。
如果一个模块经常变化就可以使用 __import__() 来动态载入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# a.py #!/usr/bin/env python #encoding: utf-8 import os print ('在 a.py 文件中 %s' % id(os)) #test.py #!/usr/bin/env python #encoding: utf-8 import sys __import__('a') # 导入 a.py 模块 |
