blog/docs/code/python/kebiancanshu.md

77 lines
1.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 函数 - 可变参数
date: 2020-09-13 17:25:10
tags: [Python]
categories: [Python]
author: Anges黎梦
---
### " *paramter "
> 表示接收任意多个实际参数,并将其放到一个元组中。
- **传递任意数量的参数**
**示例代码**
```python
def func_1(*items):
print(items)
print(type(items))
# 传递任意数量的参数
func_1(1,2,3,45,"ffff")
```
- **以列表形式传递参数**
**响应结果**
```python
(1, 2, 3, 45, 'ffff')
<class 'tuple'>
```
**示例代码**
```python
def func_1
list_1 = [1, 2, 3, 45, 'ffff','aaaa']
以列表形式传递参数
func_1(*list_1)
```
**响应结果**
```python
(1, 2, 3, 45, 'ffff', 'aaaa')
<class 'tuple'>
```
### " **paramter "
> 表示接收任意多个实际参数,并将其放到一个字典中。
- **直接传入键对值调用**
**示例代码**
```python
def func_2(**params):
print(params)
print(type(params))
func_2(a=1, b=6,c=9)
```
**响应结果**
```python
{'a': 1, 'b': 6, 'c': 9}
<class 'dict'>
```
- **通过字典传输可变参数**
**示例代码**
```python
dict_1 = {"a":1,"b":2,"c":3}
func_2(**dict_1) # 通过字典,对可变参数传值
```
**响应结果**
```python
{'a': 1, 'b': 2, 'c': 3}
<class 'dict'>
```