python默认值、关键字参数

给参数设置默认值非常有用。

def passion(name,location=" 中国"):
    return name+location

s = 'a23foiwe9owef0wfia2'
ret1 = passion("thinking",)
ret2 = passion("thinking"," 上海 浦东")

print("ret1=%s"%ret1)
print("ret2=%s"%ret2)
打印结果为:

ret1=thinking 中国
ret2=thinking 上海 浦东

从第一个调用passion方法的语句结果中可以看出,当只传一个参数时,location的值取默认值: 中国


#!/usr/bin/env python
#-*-coding:utf-8-*-
def get_per_info(name="刘十三",location=" 在中国"):
    return name+location

per_info = get_per_info(location=" 在美国")
per_info2 = get_per_info(name="快刀")

print("per_info=%s"%per_info)
print("per_info2=%s"%per_info2)

打印值是:

per_info=刘十三 在美国
per_info2=快刀 在中国


关键字参数传值可以不用考虑参数的顺序,使程序的可读性更强。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值