常用函数一
函数名 |
参数 |
介绍 |
返回值 |
举例 |
abs |
Number返回 |
数字绝对值 |
正数字 |
abs(-10) |
all |
List |
判断列表内容是否全是true |
Bool |
all(['', '123']) |
help |
object |
打印对象的用法 |
无 |
help(list) |
enumerate |
iterable |
迭代时记录索引 |
无 |
for index,item in enumerate(list) |
input |
Str |
命令行输出消息 |
Str |
lnput('请输出信息') |
In [1]: abs(-10)
Out[1]: 10
In [2]: result = all(['a' in 'abc', True, None])
In [3]: result
Out[3]: False
In [4]: result = all([True,'1', 10, len('abc')])
In [5]: result
Out[5]: True
In [6]: python = ['django', 'flask', 'torano']
In [7]: for index, item in enumerate(python):
...: print(index, item)
...:
0 django
1 flask
2 torano
In [8]: food = input('你想吃什么:')
你想吃什么:hua
In [9]: help(input)
Help on built-in function input in module builtins:
input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
常用函数二
函数名 |
参数 |
介绍 |
返回值 |
举例 |
isinstance |
Object,type |
判断对象是否是某种类型 |
Bool |
isinstance( 'a',str) |
type |
Object |
判断对象的类型 |
Str |
type(10) |
vars |
instance |
返回实例化的字典信息 |
dict |
|
dir |
object |
返回对象中所有可用方法和属性 |
List |
dir('asd') |
hasattr |
obj, key |
判断对象中是否有某个对象 |
Bool |
hasattr('1', 'upper') |
常用函数三
函数名 |
参数 |
介绍 |
返回值 |
举例 |
setattr |
Obj,key,value |
为实例化对象添加属性与值 |
无 |
setattr(instance,'run’,'go') |
getattr |
obj, key |
通过对象获取属性 |
任何类型 |
getattr(obj, key) |
any |
Iterable |
判断内容是否有true值 |
Bool |
any([1,0,' ']) |
class Test(object):
a = 1
b = 2
def __init__(self):
self.a = self.a
self.b = self.b
test =Test()
print(test.a)
result = vars(test)
print(result)
print(hasattr(test, 'a'))
print(hasattr(list, 'appends'))
setattr(test, 'c', 3)
print(test.c)
print(vars(test))
# setattr(list, 'c', 1) # 内置函数或类不能添加自定义属性
if hasattr(list, 'appends'):
print(getattr(list, 'appends')) # 没有属性会报错
else:
print('不存在')
a = ['', None, True, 0] # 存在一个True结果就为True
print(any(a))
# all -> and
# any -> or
1
{'a': 1, 'b': 2}
True
False
3
{'a': 1, 'b': 2, 'c': 3}
不存在
True
Process finished with exit code 0