我是靠谱客的博主 甜蜜嚓茶,这篇文章主要介绍Python常看函数用法,返回值类型,现在分享给大家,希望可以做个参考。

Python的函数非常多,可以使用help()函数来初略的获得函数的用法

复制代码
1
2
help(print)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
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.

同时我们自己定义函数时,也可以适当的来解释这个函数的作用

复制代码
1
2
3
4
5
6
7
def times(s:str,n:int) ->str: # 返回值为str类型 ''' 返回n个s字符串 ''' return s*n print(times('北山啦',3))
复制代码
1
2
北山啦北山啦北山啦

同时,可以使用.__annotations__方法获取函数的类型注释

复制代码
1
2
times.__annotations__
复制代码
1
2
{'s': str, 'n': int, 'return': str}

他就以字典的形式返回了他的两个参数,以及一个str类型的返回值

查看函数文档使用.__doc__方法

复制代码
1
2
print(times.__doc__)
复制代码
1
2
返回n个s字符串

在面向对象编程中,python 类有多继承特性,如果继承关系太复杂,很难看出会先调用那个属性或方法。

为了方便且快速地看清继承关系和顺序,可以使用.__mro__

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
class X(object):pass class Y(object):pass class A(X, Y):pass class B(Y):pass class C(A, B):pass print C.__mro__ # (<class '__main__.C'>, <class '__main__.A'>, # <class '__main__.X'>, <class '__main__.B'>, # <class '__main__.Y'>, <type 'object'>)

最后

以上就是甜蜜嚓茶最近收集整理的关于Python常看函数用法,返回值类型的全部内容,更多相关Python常看函数用法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部