Python 函数参数笔记
·
1 分钟阅读
·
Python
目录
函数参数类型
函数返回值取决于参数定义:
- 无前缀:返回单值
*变量:返回元组
**变量:返回字典(需传入 key=value 格式)
示例
**使用 kwargs(关键字参数)
>>> def add1(**x):
... return x
>>> add1(1,23,232321)
Traceback (most recent call last):
File "<pyshell#73>", line 1, in <module>
TypeError: add1() takes 0 positional arguments but 3 were given
>>> add1(a=23,b=22)
{'a': 23, 'b': 22}
*使用 args(可变位置参数)
>>> def add1(*x):
... return x
>>> add1(1,23,4)
(1, 23, 4)
#python
相关文章
-
Python3 的关键改进是解决了 Python2 中字符串与编码的问题。默认使用 UTF-8,区分 str(文本)和 bytes(二进制),提供清晰的 encode/decode 转换方法。
Python · 2 分钟阅读
-
Supervisor 是 Python 编写的进程管理工具,支持启动、重启、关闭单个或多个进程。介绍安装、配置 supervisord 和 program,以及使用 supervisorctl 进行进程管理的方法。
Python · 5 分钟阅读
-
多进程同时写入文件会导致内容错乱。原因是操作系统的执行单元粒度导致进程切换无序。解决方案包括加锁和使用 multiprocessing.Pool 的回调函数。
Python · 3 分钟阅读
-
Python 编码转换与中文处理完整指南,涵盖编码声明、str/unicode 类型转换、编码检测等常见问题与解决方案。
Python · 3 分钟阅读