美文网首页
python -函数参数需要明白的几点

python -函数参数需要明白的几点

作者: 浪费不过白头发 | 来源:发表于2017-03-05 16:01 被阅读13次

    1 关键字参数

    所用情形:参数太多时,防止参数顺序对函数的影响,传参时指定参数对应的形参名

    >>> def test(brand,slogan):

                   print(brand + "'s slogan is " + slogan)

    >>>test(brand='Nike',slogan='just do it')

    Nike's slogan is just do it

    2 默认参数

    >>> def test(brand='Nike',slogan='just do it'):

    print(brand + "'s slogan is " + slogan)

    >>> test()

    Nike's slogan is just do it

    3 可变参数 参数前加 *

    >>> def test(*params):

    print('参数的长度是:',len(params) )

    >>> test(1,2,3,4)   #实现形式参数打包成元组

    参数的长度是: 4

    相关文章

      网友评论

          本文标题:python -函数参数需要明白的几点

          本文链接:https://www.haomeiwen.com/subject/waynwttx.html