zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【Java学习笔记】<集合框架>Iterator的子接口ListIterator
    【Java学习笔记】<集合框架>List特有的取出方式之一
    【Java学习笔记】集合框架Ⅱ
    【Java学习笔记】集合框架Ⅰ
    【PS】Ⅱ图像合成与渐变工具笔记
    【PS】Ⅰ基础及选框工具笔记
    [PS]简单的智能电视制作案例
    Spring中线程池的使用
    SpringBoot 多线程
    solr DIH 设置定时索引
  • 原文地址:https://www.cnblogs.com/raphael5200/p/5114717.html
Copyright © 2011-2022 走看看