Python学习笔记7-把函数当参数传递、指定可变参数

把函数当参数传递

# 函数参数传递
# 面向对象编程就是把对象传来传去
# 面向函数编程就是把函数传来传去

def mytest(num):
     return num * 2
     
# # 不光可以传递变量,还可以传递函数
def convert(func, seq):
   print 'convert sequence of numbers to same type'
   return [func(eachNum) for eachNum in seq]
     
myseq = [123, 45.67, -6.2e8, 99999999L]
# # 面向对象编程说白了就是把对象传来传去,对象是第一要素
# # 面试函数编程说白了就是把函数传来传去,函数是第一要素
print convert(int, myseq)
print convert(long, myseq)
print convert(float, myseq)
print convert(mytest, myseq)


函数的递归:

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00137473836826348026db722d9435483fa38c137b7e685000


指定可变参数

#--encoding:utf-8--

print '---------------给参数指定默认值-----------------------'
def taxMe(cost,rate=0.85):
    return cost * rate

print taxMe(5) #4.25

print '---------------给有默认值的参数指定值-----------------------'

print taxMe(5,2) #10

#在Python函数中,还可以定义可变参数。顾名思义,可变参数就是传入的参数个数是可变的,可以是1个、2个到任意个,还可以是0个
#可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple
print '---------------给函数指定可变参数(参数个数不确定)-----------------------'

def taxMe2(cost,rate=0.85,*theRest):
    for aRest  in theRest:
        cost+=cost+aRest
        print 'arg:',aRest
    return cost+cost*rate

print taxMe2(10, 0.5,10,11,12,13,14)

#而关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict。
print '---------------给函数指定关键字参数(参数个数不确定)------------------------'

def taxMe3(cost,rate=0.85,**theRest):
    for key in theRest.keys():
        cost +=theRest[key]
        print key,':',theRest[key]
    return cost+cost*rate
print taxMe3(00, 0.05, electric=100, water=200, gas=300)

#在Python中定义函数,可以用必选参数、默认参数、可变参数和关键字参数,
#这4种参数都可以一起使用,或者只用其中某些,但是请注意,参数定义的顺序必须是:必选参数、默认参数、可变参数和关键字参数。
print '--------------给函数指定可变参数+关键字参数------------------------'

def taxMe4(cost,rate=0.85,*theRest,**theRest2):
    cost2=0
    for forRest in theRest:
        cost2+=forRest
        print 'theRest:',forRest
    for key in theRest2.keys():
        cost+=theRest2[key]
        print 'theRest2:key:',key,',value:',theRest2[key]
    return cost+cost2+(cost+cost2)*rate

print taxMe4(100, 50,100,150,300, electric=100 ,water=200,gas=30)
结果:

---------------给参数指定默认值-----------------------
4.25
---------------给有默认值的参数指定值-----------------------
10
---------------给函数指定可变参数(参数个数不确定)-----------------------
arg: 10
arg: 11
arg: 12
arg: 13
arg: 14
984.0
---------------给函数指定关键字参数(参数个数不确定)------------------------
water : 200
gas : 300
electric : 100
630.0
--------------给函数指定可变参数+关键字参数------------------------
theRest: 100
theRest: 150
theRest: 300
theRest2:key: water ,value: 200
theRest2:key: gas ,value: 30
theRest2:key: electric ,value: 100
49980

更多关于函数参数的帮助:

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001374738449338c8a122a7f2e047899fc162f4a7205ea3000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值