Python中函数的参数类型实例分析

发布时间:2022-07-12 14:05:44 作者:iii
来源:亿速云 阅读:137

今天小编给大家分享一下Python中函数的参数类型实例分析的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

1、Python的函数参数的类型

2、Python的必传参数

# coding:utf-8
# Author:YangXiaoPeng
def demo01(a,b):
print(a, type(a))
print(b, type(b))
# demo01(1) #TypeError missing 1 required positional argument: 'b' 缺少一个未知的参数,demo01必须传2个参数
# demo01(1, 2, 3) #TypeError demo01() takes 2 positional arguments but 3 were given, demo01函数有2个位置参数,但是接收到了3个;
# 必传参数 : 调用函数时必须传入的参数,函数定义时只定义参数名
# 传入的参数个数必须与形参的数量一致
demo01(1, 2) # yes
demo01(1, [1, 2]) # yes
demo01([2, 3], (1, 2)) # yes
demo01(1, {2, 3, 4}) # yes
demo01(2, {"code":'1001', "name":"zhang", "age":18}) # yes

Python中函数的参数类型实例分析

3、关键字参数

# coding:utf-8
# Author:YangXiaoPeng
def demo01(a,b):
print(a, type(a), end="__")
print(b, type(b))
# 第一个关键字出入的参数位置之前的参数比逊选择关键字传参;如下面的示例中,第一个关键字传参的变量是a,a在函数定义的第二个位置,那么第二个位置之前的参数都必须以关键字传参的形式传参。
# demo01(1, a=2) # demo01() got multiple values for argument 'a'
demo01(a=1, b=2) # yes
demo01(1, b=2) # yes
demo01(b=1, a=2) # yes
demo01(b=1, a=[1, 2]) # yes
demo01(b=[2, 3], a=(1, 2)) # yes
demo01(b=1, a={2, 3, 4}) # yes
demo01(b=2, a={"code":'1001', "name":"zhang", "age":18}) # yes

Python中函数的参数类型实例分析

4、默认参数

# coding:utf-8
# Author:YangXiaoPeng
## 默认参数
def demo02(City = "LongNan"):
print("City是默认参数,默认值是:中国,当前值是:", City)
# 不传入参数
demo02()
# 传入参数
demo02("Beijing")

Python中函数的参数类型实例分析

5、不定长参数

# coding:utf-8
# Author:YangXiaoPeng
## 不定长参数
def demo03(*args):
print(args,type(args))
# 传入的参数会生成一个元组类型的变量供函数内部使用
demo03(1)
demo03("code")
demo03(1,"code")

Python中函数的参数类型实例分析

# coding:utf-8
# Author:YangXiaoPeng
# *args 后面的形参,必须以关键字参数进行传参,
def demo04(a, b, *args,c):
print("a传入的参数值是:{},b传入的参数值是:{}, args传入的参数是:{}, c出入的参数是:{}".format(a, b, args,c))
# 传入的参数不能少于必传参数的个数,a,b,c三个为必传参数
# demo04(1, 2) # TypeError
# demo04(1, 2, 3) # TypeError
demo04(1, 2, c=3)

Python中函数的参数类型实例分析

Python中函数的参数类型实例分析

# coding:utf-8
# Author:YangXiaoPeng
def demo05(**kwargs):
print("kwargs传入的参数是:{}".format(kwargs),type(kwargs))
kwargs = {"code":'1002', "name":"zhang"}
# demo05(kwargs) # TypeError
# **修饰的参数必须以关键字的参数方式传参,Python解释器会将传入的关键字和关键字的值生成一个字典供函数内部使用
demo05(**kwargs) # kwargs传入的参数是:{'code': '1002', 'name': 'zhang'} <class 'dict'>
demo05(code='1002',name="zhang") # kwargs传入的参数是:{'code': '1002', 'name': 'zhang'} <class 'dict'>

Python中函数的参数类型实例分析

# coding:utf-8
# Author:YangXiaoPeng
# **修饰的参数必须是最后一个
"""
# SyntaxError: invalid syntax
def demo06(a, b, *args, c, **kwargs, d):
pass
"""
def demo06(a, b, *args, c, **kwargs):
print("a传入的参数值是:{},b传入的参数值是:{}, args传入的参数是:{}".format(a, b, args),end='')
print(",c传入的参数是:{},kwargs传入的参数是:{}".format(c,kwargs))
demo06(1, 2, 3, 4, 5, c=3, code='1002', name="zhang", d=3)

Python中函数的参数类型实例分析

以上就是“Python中函数的参数类型实例分析”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. Python参数类型以及常见的坑
  2. python函数中参数类型的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:python四则运算表达式求值的方法

下一篇:如何使用Python去除小数点后面多余的0

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》