zoukankan      html  css  js  c++  java
  • 函数的参数设定

    1.def test(x, y):

         print(x, y)

    test(y=1, x=2)

    test(2 , y=1)    #位置参数一定要在关键字参数的前面 

    def test(x, y, z):

        print(x, y, z)

    test(2, z=1, y=2)

    2.默认参数

    def test(x, y=2):   #定义关键字参数时,位置参数要在关键字参数之前 不能写成 test(y=2, x)

         print(x, y) 

    test(2)

    test(2,2)

    3.传递不固定的形参 

    def test(*args):
          print(test)

    test(1, 2, 3, 4, 5, 6)

    test(*[1, 2, 3, 4, 5, 6])  #*args = *[1, 2, 3, 4, 5] #传入一个元组

    test([1, 2, 3, 4, 5, 5])    #传入一个列表 为单独的一个变量 

    def test(x, *args):

          print(x)

          print(args)

    4. 传递字典 

    def test3(name, **kwargs):

          print(name)

          print(kwargs)

    test('alex', age=18, sex='m')

    def test3(name, **kwargs):

          print(name)

          print(kwargs['age'])

          print(kwargs['sex'])

    test('alex', age=18, sex='m')

    5.混合编程

    def test4(name, *args, age = 18, **kwargs):

           print(name)

           print(age)

           print(kwargs)

           logger('Test4')

    def logger(source):

           print('from %s' %source)

    test('alex', 18, age = 18, sex = 'm', hobby = 'tesla)

  • 相关阅读:
    UrlRewriter配置IIS支持伪静态
    Linux 安装PAE内核
    Tmux入门教程
    Tmux与Oh-my-zsh环境整合
    MySQL Route负载均衡与读写分离Docker环境使用
    MySQL数据表的基本操作
    Git安全配置
    GitLab使用自定义端口
    Gitlab搭建安装及使用中遇到的问题。
    执行Docker命令报错解决办法
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/9036811.html
Copyright © 2011-2022 走看看