zoukankan      html  css  js  c++  java
  • Python定义函数

    1. 其他形式1:

      1、定义函数

      def test4(a = ()):

          print('################test4################')

          print(type(a))

          print(a)

      2、调用函数

      正确调用:

      test4((1, 2))        #a在函数体内部为tuple类型

      test4(a=(1, 2))    #a在函数体内部为tuple类型

      test4((1,))          #a在函数体内部为tuple类型

      test4(a=(1,))      #a在函数体内部为tuple类型

      test4((1))           #a在函数体内部为int类型,非tuple类型

      test4(a=(1))       #a在函数体内部为int类型,非tuple类型

      test4(1)             #a在函数体内部为int类型,非tuple类型

      test4(a=1)         #a在函数体内部为int类型,非tuple类型

      错误调用:

      test4(1, 2)          #TypeError: test4() takes at most 1 argument (2 given)

      test4(1, b=2)      #TypeError: test4() got an unexpected keyword argument 'b'

      test4(a=1, b=2)  #TypeError: test4() got an unexpected keyword argument 'b'

    2. 5

      其他形式2:

      1、定义函数

      def test5(b = {}):

          print('################test5################')

          print(type(b))

          print(b)

      2、调用函数

      正确调用:

      test5({'a':2})       #b在函数体内部为dict类型

      test5(b={'a':2})

      test5({'a':2,'b':3})#b在函数体内部为dict类型

      test5(b={'a':2,'b':3})

      test5(b=2)           #b在函数体内部为int类型,非dict类型

      错误调用:

      test5(a=1, b=2)   #TypeError: test5() got an unexpected keyword argument 'a'

      test5(1, 2)           #TypeError: test5() takes at most 1 argument (2 given)

      test5(1, b=2)       #TypeError: test5() got multiple values for keyword argument 'b'

    3. 6

      其他形式3:

      1、定义函数

      def test6(a = (), b = {}):

          print('################test6################')

          print(type(a))

          print(a)

          print(type(b))

          print(b)

      2、调用函数

      正确调用:

      test6(1, 2)

      test6(a=1, b=2)

      test6(a=1, b=2)

      test6((1, 2), {'c':8})

      test6({'c':8})

      test6(b={'c':8})

      test6((1, 2), b=2)

      test6((1, 2), b=2)

      错误调用:

      test6(a=1, 2)       #SyntaxError: non-keyword arg after keyword arg

      test6(1, 2, b=2)   #TypeError: test6() got multiple values for keyword argument 'b'

  • 相关阅读:
    广告电商系统开发功能和源码分享
    定义curl方法 请求接口传输post值,设置header值
    php json保存为utf8
    超越自卑(阿德勒)阅读笔记
    最近的一些事
    BN.2021.1007.1131.简明的Tensorflow2.0
    RX.2021.1004.1546.三维重建.单张图获取深度信息
    RX.2021.1004.1544.图像配准.基于VoxelMorph的脑部MRI配准
    RX.2021.0909.1408.图像分割.基于UNet的OCT血管分割
    RX.2021.0903.1118.图像去噪.基于自监督深度学习的神经活动荧光图像去噪
  • 原文地址:https://www.cnblogs.com/MrZhang1/p/6628565.html
Copyright © 2011-2022 走看看