zoukankan      html  css  js  c++  java
  • Python函数定义及参数详解

    函数定义

    首先我们来创建一个函数,输出指定范围内的斐波拉契数列(Fibonacci series)。

    #!/usr/bin/env python 
    #coding=utf-8
    '''
    Created on 2016年9月4日下午2:37:31
    @author: Flowsnow
    @file: D:/Workspaces/eclipse/HelloPython/main/FibonacciSeries.py
    @function: 定义函数-输出给定范围内的斐波拉契数列
    '''
    def Fibonacci(n):
        #print "success"
        a=0
        b=1
        while a<n:
            print a,
            a,b=b,a+b
    
    #call the function Fibonacci
    Fibonacci(2000)
    print '
    ',
    print Fibonacci
    f=Fibonacci
    f(100)
    print '
    ',
    print Fibonacci(0)
    

    输出结果如下:

    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 
    <function Fibonacci at 0x000000000258D9E8>
    0 1 1 2 3 5 8 13 21 34 55 89 
    None
    

    由第一行可知 Fibonacci函数输出了2000以内的斐波拉契数列。

    由第二行可知 Fibonacci函数在内存中的地址

    由第三行可知 将Fibonacci函数的地址值赋给另外一个变量f之后,f也就是一个函数了,这类似于重名机制

    由第四行可知 虽然Fibonacci函数没有return语句,但是如果我们使用print输出的时候可以发现还是有返回值的,只是这个返回值是None,这是Python的內建名称。

    我们也可以写一个函数,不输出斐波拉契数列的值,而是把值作为返回值返回。

    #!/usr/bin/env python 
    #coding=utf-8
    '''
    Created on 2016年9月4日下午3:07:06
    @author: Flowsnow
    @file: D:/Workspaces/eclipse/HelloPython/main/FibonacciSeriesAdv.py
    @function: 函数定义-返回斐波拉契数列,而不是直接打印
    '''
    def Fibonacci(n):
        a=0
        b=1
        result=[]
        while a<n:
            result.append(a)
            a,b=b,a+b
        return result
    result=Fibonacci(2000)
    for x in result:
        print x, 
    

    输出结果:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

    参数详解

    Python 的内建标准类型有一种分类标准是分为可变类型与不可变类型

    • 可变类型:列表、字典
    • 不可变类型:数字、字符串、元组

    上面函数定义中的参数都是属于不可变类型的。

    可变参数三种情况:默认参数,位置参数*args关键字参数**kwargs

    默认参数

    默认参数的好处就是在调用函数的时候写上去的参数比在函数定义时的参数少。例如:

    #!/usr/bin/env python 
    #coding=utf-8
    '''
    Created on 2016年9月5日下午2:50:12
    @author: Flowsnow
    @file: D:/Workspaces/eclipse/HelloPython/main/askYesOrNo.py
    @function: 测试默认参数的使用
    '''
    def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
        while True:
            ok = raw_input(prompt)
            if ok in ('y', 'ye', 'yes'):
                return True
            if ok in ('n', 'no', 'nop', 'nope'):
                return False
            retries = retries - 1
            if retries < 0:
                raise IOError('refusenik user')
            print complaint
    

    这个函数的调用方法有很多,比如:

    • 只给必选参数:ask_ok('OK to overwrite the file?')
    • 给一个可选参数:ask_ok('OK to overwrite the file?', 2)
    • 给所有的可选参数:ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')

    关于默认值,应该注意的是默认值只会在函数定义的时候被python解析一次。因此

    i = 5
    
    def f(arg=i):
        print arg
    
    i = 6
    f()
    

    这段代码输出的应该是5,而不是6,就是因为i是在函数定义的时候解析的,这个时候i=5。

    重要警告:默认值只会解析一次。当默认参数是可变对象时,影响比较大,比如列表,字典或者类的对象。下面演示的这个函数会把参数积累并传到随后的函数调用里面:

    def f(a, L=[]):
        L.append(a)
        return L
    
    print f(1)
    print f(2)
    print f(3)
    

    这段代码会输出

    [1]
    [1, 2]
    [1, 2, 3]
    

    如果不想默认参数在后面的函数调用中共享,可以把函数写成这种形式

    def f(a, L=None):
        if L is None:
            L = []
        L.append(a)
        return L
    

    这段代码会输出

    [1]
    [2]
    [3]
    

    位置参数*args

    位置参数需要在参数前面加一个星号。把参数收集到一个元tuple中,作为变量args。至于为什么叫位置参数,这个是因为各个参数是按照顺序接收的。

    def argTest(arg1,*args):
        print arg1
        print('~start to print *args~')
        for x in args:
            print x,
    
    argTest(1,'two',3)
    

    这段代码会输出

    1
    ~start to print *args~
    two 3
    

    args被解释为包含多个变量的元组tuple。因此也可用如下写法:

    def argTest(arg1,*args):
        print arg1
        print('~start to print *args~')
        for x in args:
            print x,
    
    #argTest(1,'two',3)
    args=['two',3]
    argTest(1,*args)
    

    关键字参数**kwargs

    函数也能够按照kwarg=value这种形式的关键字参数来调用。关键字参数需要在参数前面加两个星号。其作用是把参数收集成一个字典类型,包含参数名和值。

    def argTest(arg1,**kwargs):
        print 'arg1',arg1
        for key in kwargs:
            print key,kwargs[key]
    argTest(1,arg2='aa',arg3='bb')
    argTest(arg1=1,arg2='aa',arg3='bb',arg4='cc')
    arg={'arg2':'bb','arg3':'cc','arg4':'dd'}
    argTest(arg1='ss',**arg)
    argTest(arg1='ss',**arg)
    

    这段代码会输出

    arg1 1
    arg2 aa
    arg3 bb
    arg1 1
    arg2 aa
    arg3 bb
    arg4 cc
    arg1 ss
    arg2 bb
    arg3 cc
    arg4 dd
    arg1 ss
    arg2 bb
    arg3 cc
    arg4 dd
    

    参考资料

    Python官网-defining-functions

    Passing arguments to Python functions1.pdf

    Python中*args与**args的区别


    记得帮我点赞哦!

    精心整理了计算机各个方向的从入门、进阶、实战的视频课程和电子书,按照目录合理分类,总能找到你需要的学习资料,还在等什么?快去关注下载吧!!!

    resource-introduce

    念念不忘,必有回响,小伙伴们帮我点个赞吧,非常感谢。

    我是职场亮哥,YY高级软件工程师、四年工作经验,拒绝咸鱼争当龙头的斜杠程序员。

    听我说,进步多,程序人生一把梭

    如果有幸能帮到你,请帮我点个【赞】,给个关注,如果能顺带评论给个鼓励,将不胜感激。

    职场亮哥文章列表:更多文章

    wechat-platform-guide-attention

    本人所有文章、回答都与版权保护平台有合作,著作权归职场亮哥所有,未经授权,转载必究!

  • 相关阅读:
    leetcode231 2的幂 leetcode342 4的幂 leetcode326 3的幂
    leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
    leetcode64. Minimum Path Sum
    leetcode 20 括号匹配
    算法题待做
    leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown 、714. Best Time to Buy and Sell Stock with Transaction Fee
    rand7生成rand10,rand1生成rand6,rand2生成rand5(包含了rand2生成rand3)
    依图
    leetcode 1.Two Sum 、167. Two Sum II
    从分类,排序,top-k多个方面对推荐算法稳定性的评价
  • 原文地址:https://www.cnblogs.com/CHLL55/p/13733853.html
Copyright © 2011-2022 走看看