zoukankan      html  css  js  c++  java
  • python-第01章07-03章节-函数与函数式编程之参数详解

    参数:

    1.形参和实参

    形参:形式参数,不是实际的存在,是虚拟变量。在定义函数和函数体的时候使用形参,nudity是在函数调用的时候接受实参(实参个数,类型应与实参一一对应)

    实参:实际参数,调用函数的时候传给函数的参数,可以是变量,常量,表达式,函数,传给形参。

    区别:形参是虚拟的,不占用内存空间,实参是变量,占用空间。

    顺序:实参传给形参。

    2.位置参数和关键字

    位置参数 (个人理解):实参的参数会按照顺序传给的形参。

    关键字:将一个参数设置为已知参数

    def test(x,y):
      print(x)
      print(y)
    test(1,y=2)
    #x为位置参数,y为关键字

      结果:

    1
    2
    

      

    3默认参数:直接赋值。

    特点:调用函数的时候,默认参数非必须传递。

    def test(x,y=3):
      print(x)
      print(y)
    test(1)
    

      结果:

    1
    3
    

    4.参数组

    作用:可以传输多个不固定的参数

    def test(*args):
      print(args)
    test(1,2,3,4,5)
    #1,2,3,4,5=args
    test(*[1,2,3,4,5])
    #*[1,2,3,4,5]=*args
    #*代表一个功能代号,表示是我后面的东西会不固定
    

      结果;

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

      用字典的方式使用参数组:

    def test2(**kwargs):
      print(kwargs)
    test2(name='hanjiali',age='8',sex='girl')
    
    #**kwargs:把关键字参数转换为字典的方式
    

      结果:

    {'age': '8', 'name': 'hanjiali', 'sex': 'girl'}
    

      

  • 相关阅读:
    【Codeforces 349B】Color the Fence
    【Codeforces 459D】Pashmak and Parmida's problem
    【Codeforces 467C】George and Job
    【Codeforces 161D】Distance in Tree
    【Codeforces 522A】Reposts
    【Codeforces 225C】Barcode
    【Codeforces 446A】DZY Loves Sequences
    【Codeforces 429B】Working out
    【Codeforces 478C】Table Decorations
    【Codeforces 478C】Table Decorations
  • 原文地址:https://www.cnblogs.com/hanjiali/p/11658475.html
Copyright © 2011-2022 走看看