详解python内置函数(二)
- 1.函数目录
- 2.函数详解
- (1)help
- (2)sum
- (3)dir
- (4)bytes
- (5)all
- (6)any
- (7)enumerate
- (8)zip
- (9)filter
- (10)map
- (11)sorted
- (12)callable
- (13)globals
- (14)locals
- (15)getattr
- (16)hasattr
- (17)delattr
- (18)setattr
- (19)iter
- (20)next
- (21)super
1.函数目录
函数名 |
功能 |
help |
查看指定对象的帮助信息 |
sum |
返回迭代对象相加的结果 |
dir |
列出指定对象的属性信息 |
bytes |
把字符串转为bytes |
all |
判断可选代对象的每个元素是否都为True值,返回布尔类型 |
any |
判断可迭代对象的元素是否有为True值的元素,返回布尔类型 |
enumerate |
用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标 |
zip |
合并多个序列类型 |
filter |
根据提供的函数返回为真的一个新序列 |
map |
根据提供的函数对指定序列做映射 |
sorted |
对指定的序列进行排序 |
callable |
用来检测对象是否可被调用,返回布尔类型 |
globals |
函数会以字典格式返回当前位置的全部全局变量 |
locals |
返回本地作用域中的所有名字 |
getattr |
函数用于返回一个对象属性值 |
hasattr |
用于判断对象是否包含对应的属性 |
delattr |
用于删除属性 |
setattr |
用于设置属性值,该属性不一定是存在的 |
iter |
用来生成迭代器 |
next |
返回可迭代对象中的下一个元素值 |
super |
用于调用父类(超类)的一个方法 |
2.函数详解
(1)help
help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
(2)sum
sum([11,22,33])
sum([11,22,33],100)
66
166
(3)dir
dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
(4)bytes
bytes("内置函数",encoding="utf-8")
b'\xe5\x86\x85\xe7\xbd\xae\xe5\x87\xbd\xe6\x95\xb0'
(5)all
- 功能:判断可选代对象的每个元素是否都为True值,返回布尔类型
all([11,22,33])
all([11,22,33,0])
all([11,22,33,[]])
True
False
False
(6)any
- 功能:判断可迭代对象的元素是否有为True值的元素,返回布尔类型
any([11,0,0,0])
any([0,0,0,0,0])
True
False
(7)enumerate
- 功能:用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标
for i in enumerate([11,22,33]):
print(i)
(0, 11)
(1, 22)
(2, 33)
(8)zip
a=zip([1,2,3],["a","b","c"])
a
list(a)
b=zip([1,2,3],["a","b"])
list(b)
<zip object at 0x000001737E5D3A00>
[(1, 'a'), (2, 'b'), (3, 'c')]
[(1, 'a'), (2, 'b')]
(9)filter
- 功能:根据提供的函数返回为真的一个新序列
- 相当于过滤器
a=[1,2,3,4,5]
temp=filter(lambda x:x%2==0,a)
temp
list(temp)
def f(x):
return x%2==0
temp=filter(f,a)
list(temp)
<filter object at 0x000001737E54F5E0>
[2, 4]
[2, 4]
(10)map
- 功能:根据提供的函数对指定序列做映射
- 相当于映射器
temp=map(lambda x:x+1,[1,2,3])
temp
list(temp)
temp=map(lambda x,y:x+y,[1,2,3],[11,22,33])
list(temp)
<map object at 0x00000148DBB48790>
[2, 3, 4]
[12, 24, 36]
(11)sorted
sorted([22,5,9,11])
sorted([22,5,9,11],reverse=True)
dt={"huawei":123,"zw":45,"lenovo":78}
sorted(dt.items(),key=lambda x:x[1])
sorted(dt.items(),key=lambda x:x[1],reverse=True)
[5, 9, 11, 22]
[22, 11, 9, 5]
[('zw', 45), ('lenovo', 78), ('huawei', 123)]
[('huawei', 123), ('lenovo', 78), ('zw', 45)]
(12)callable
a=1
def f(x):
return x%2==0
callable(a)
callable(f)
callable(print)
False
True
True
(13)globals
a=1
print(globals())
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1}
(14)locals
def add(a,b):
print(locals())
return a+b
add(11,22)
{'a': 11, 'b': 22}
(15)getattr
a=[11,22,33]
getattr(a,"append")
getattr(a,"append")(55)
a
<built-in method append of list object at 0x00000236F36D6AC0>
[11, 22, 33, 55]
(16)hasattr
a=[11,22,33]
hasattr(a,"append")
hasattr(a,"append1")
True
False
(17)delattr
class A(object):
def __init__(self, name, age):
self.name = name
self.age = age
a = A("hhh", 66)
print dir(a)
delattr(a, "age")
print dir(a)
['__doc__', '__init__', '__module__', 'age', 'name']
['__doc__', '__init__', '__module__', 'name']
(18)setattr
class A(object):
name = "xiaoniao"
a = A()
setattr(a, "age", 33)
print(a.age)
33
(19)iter
a=[11,22,33]
type(a)
a=iter(a)
type(a)
<class 'list'>
<class 'list_iterator'>
(20)next
a=[11,22,33]
a=iter(a)
next(a)
next(a)
next(a)
11
22
33
(21)super
class A(object):
def __init__(self):
print("A1")
print("A2")
class B(A):
def __init__(self):
print("B1")
super(B, self).__init__()
print("B2")
b=B()
B1
A1
A2
B2