python天阶教程--函数参数

        Python函数很自由,其中函数参数就够喝一壶的,不多说,上代码:

# 1.函数默认值参数,给参数指定默认值,在调用时开业减少参数个数
def ask_ok(prompt, retries=4, reminder="please try again"):
    while True:
        ok = input(prompt)
        if ok in ('y', 'ye', 'yes'):
            return True
        if ok in ('n', 'no', 'nopt'):
            return False
        retries = retries - 1
        if retries < 0:
            raise ValueError('invalid user response')
        print(reminder)


#  函数调用:
# 只给出必须实参
ask_ok('Do you really want to quit?')
# 给出一个可选实参
ask_ok('ok to overwrite the file', 2)
# 给出所有参数
ask_ok('ok to overwrite the file?', 2, 'come on only yes or no!')

"""

"""


# 2.关键字参数,提供形如kwarg=value的参数可以调用函数
def parrot(voltage, state='hello', action='voom', type='Norwegian bule'):
    print("--this parrot wouldnot", action, end=' ')
    print("if you put", voltage, "volts through it")
    print("--Loverly plumage the", type)
    print("--Itis", state, "!")


#  该函数可以接受一个必选参数voltage和三个可选参数state,action,type,调用方式有:
parrot(10000)
parrot(voltage=1000)
parrot(voltage=1000, action='hello world')
parrot(action='hello world', voltage=1000)
parrot('hello', 'hello world', 'jump')
# 调用时不能无参数,不能出现相同的参数,不能出行函数中不存在的参数,关键字参数必须跟在位置参数后面

"""
"""


# 3.当参数形如**name时接收一个字典参数,形如*name时接收一个元组参数
def cheeseshop(kind, *arguments, **keywords):
    print("--想来点啥", kind, '?')
    print("--哎呀,买完了!", kind)
    for arg in arguments:
        print(arg)
    print("--" * 20)
    for kw in keywords:
        print(kw, ":", keywords[kw])


# 调用方式:
cheeseshop("李明",
           "我的天啊!",
           "为什么?",
           shopkeeper="店员",
           client="顾客",
           sketch="欧了")

''' 
输出:
--你还有什么问题吗 李明 ?
--不好了,没时间了 李明
我的天啊!
为什么?
----------------------------------------
shopkeeper : 店员
client : 顾客
sketch : 欧了
--你还有什么问题吗 李明 ?
--不好了,没时间了 李明
我的天啊!
为什么?
----------------------------------------
shopkeeper : 店员
client : 顾客
sketch : 欧了
'''

# 4.特殊参数
"""
函数中没有定义/或*时,参数可以按位置或关键字传递给函数
仅限位置参数:当为仅限位置参数时,参数位置很重要,不能使用关键字参数,且参数在/前定义,以区分其他形参,
仅限位置形参的位置固定所以不用形参名也知道参数,可以让用户无法使用形参名
仅限关键字参数:当为仅限关键字参数,必须在参数列表的第一个形参前加*
"""


# (1)普通参数
def stand_arg(arg):
    print(arg)


# 调用:可以使用参数名
stand_arg(100)
stand_arg(arg=200)


# 输出 100  200


# (2)仅限位置参数
def pos_only_arg(arg, arg2, /):
    print(arg)
    print(arg2)


# 调用:不能调用参数名,/前两个都是仅限位置参数
pos_only_arg(1, 2)  # 错误调用:post_only-ary(arg=1)


# (3)仅限关键字参数
def kwd_only_arg(*, arg, arg2):
    print(arg)
    print(arg2)


# 调用:顺序可变
kwd_only_arg(arg2=100, arg=200)
kwd_only_arg(arg=100, arg2=200)


# 错误: kwd_only_arg(100,300)

# (4)都包含
def combined_example(pos_only, /, standard, *, kwd_only):
    print(pos_only, standard, kwd_only)


# 调用:顺序不可变
combined_example(1, 2, kwd_only=3)
combined_example(1, standard=2, kwd_only=3)

# 5.解包实参数
"""
函数调用要求是独立的位置参数,但实参在列表或元组里时,要执行相反的操作,例如参数在
列表或元组中时,用*操作把实参解包出来
"""


def sumg(arg):
    print(arg)


arg1 = [1, 5]
sumg(range(*arg1))

# 如果是字典要用**解包
dict1 = {'1': 'V', '2': 'M'}
sumg(**dict1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前段被迫创业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值