08 - 函数一

08 - 函数一

1. 函数简介
  • 函数也是一个对象

    • 对象保存了三个数据:

      1. id (标识、地址)
      2. type(类型)
      3. value(值)
      • a = [1,2,3] 
        print(a,id(a))
        a[0] = 10  # value值改变了
        print(a,id(a))
        # 这个操作不会改变变量所指向的对象 ( id不会改变 )
        # [1, 2, 3]  1942413817288
        # [10, 2, 3] 1942413817288
        
        a = [1,2,3] 
        b = a 
        print(a,id(a))
        print(b,id(b))
        b[0] = 10
        print(a,id(a))
        print(b,id(b))
        # [1, 2, 3]  1942419623368
        # [1, 2, 3]  1942419623368
        # [10, 2, 3] 1942419623368
        # [10, 2, 3] 1942419623368
        
  • 函数是用来保存一些可执行的代码,并且可以在需要时,对这些语句进行多次调用

  • 语法

    • def 函数名([形参1,形参2,形参3.....]):
      	代码块
      
      • 注意:

      • 函数名必须符合标识符的规范(可以包含字母、数字、下划线但是不能以字母开头)

      • print 是函数对象, print() 是调用函数

        • def fn():
              print('这是我的第一个函数')
              
          print(fn)
          # <function fn at 0x000001C4407E69D8>
          
          fn()
          # 这是我的第一个函数
          
          print(fn()) # 打印fn的功能
          # 这是我的第一个函数
          # None
          
2. 函数的参数
2.1 形参和实参
  • 形参(形式参数)定义形参相当于在函数内部声明了变量,但是并不是赋值

  • 实参(实际参数)指定了形参,在调用函数时必须传递实参,实参将会赋值给对应的形参,简单来说有几个形参就要有几个实参

    • def s():
          print(1+1)
      s()
      # 2
      
      def fn(a,b):
          print(a + b)
      fn()
      # TypeError: fn() missing 2 required positional arguments: 'a' and 'b'
      
      def fn(a,b):
          print(a + b)
      fn(5)
      # TypeError: fn() missing 1 required positional argument: 'b'
      
      def fn(a,b):
          print(a + b)
      fn(5,300)
      # 305
      
2.2 函数的传递方式
  • 定义形参时,可以为形参指定默认值。指定了默认值以后,如果用户传递了参数则默认值不会生效。如果没有传递,则默认值会生效

    • def fn(a,b,c=20):
          print('a = ',a)
          print('b = ',b)
          print('c = ',c)
      fn(1,2)
      fn(1,2,3)
      # a =  1
      # b =  2
      # c =  20
      # a =  1
      # b =  2
      # c =  3
      
  • 位置参数:是将对应位置的实参赋值给对应位置的形参

    • def fn(a = 5,b = 6,c =10):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn(1,2,3)
      # a = 1
      # b = 2
      # c = 3
      
      fn(1,2)
      # a = 1
      # b = 2
      # c = 10
      
      fn()
      # a = 5
      # b = 6
      # c = 10
      
  • 关键字参数:可以不按形参定义的顺序进行传递,而根据参数名进行传递

    • def fn2(a ,b ,c ):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn2(b=1,c=2,a=3)
      # a = 3
      # b = 1
      # c = 2
      
  • 混合使用位置参数和关键字参数必须将位置参数写到关键字参数前面

    • def fn2(a ,b ,c ):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn(1,2,c=20)
      # a = 1
      # b = 2
      # c = 20
      
      def fn2(a ,b ,c ):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn2(c=20,1,2)
      # SyntaxError: positional argument follows keyword argument
      
  • 拓展

    • def fn2(a):
          print('a = ',a)
      b = 456
      c = [1,2,3]
      d = True
      e = 'python'
      fn2(b)
      fn2(c)
      fn2(d)
      fn2(e)
      # a =  456
      # a =  [1, 2, 3]
      # a =  True
      # a =  python
      
      def fn3(a,b):
          print(a + b)
      fn3(1,'2')
      # TypeError: unsupported operand type(s) for +: 'int' and 'str'
      
      def fn4(a):
          print('a = ',a,id(a))
      c = 10
      fn4(c)
      print('c = ',c,id(c))
      # a =  10 140718008804016
      # c =  10 140718008804016
      
      def fn4(a):
          a = 20 
          print('a = ',a,id(a))
      c = 10
      fn4(c)
      print('c = ',c,id(c))
      # a =  20 140718008804336
      # c =  10 140718008804016
      
      def fn4(a):
          a[0] = 10
          print('a = ',a,id(a))
      c = [1,2,3]
      fn4(c)
      print('c = ',c,id(c))
      # a =  [10, 2, 3] 1942421991752
      # c =  [10, 2, 3] 1942421991752
      
      def fn4(a):
          a[0] = 10
          print('a = ',a,id(a))
      c = [1,2,3]
      fn4(c.copy())
      print('c = ',c,id(c))
      # a =  [10, 2, 3] 1942406111880
      # c =  [1, 2, 3] 1942421366600
      
3. 不定长参数
  • 定义函数时,可以在形参前加一个*,这样这个形参可以获取到所有的实参,它会将所有的实参保存到一个元组中

    • # 定义函数时,可以在形参前面加一个*,这样这个形参可以获取到所有的实参,
      # 它会将所有的实参保存到一个元组中
      def fn(*b):
          print('b =',b,type(b))
      # *b 会接受所有的位置实参,并且会将这些实参统一保存到一个元组当中
      fn(1,2,3,4,5,6,7,8)
      # b = (1, 2, 3, 4, 5, 6, 7, 8) <class 'tuple'>
      
      def s(*b):
          # 定义一个变量 保存结果
          r = 0
          # 遍历元组,并将元组中的数进行累加
          for i in b:
              r += i
          print(r)
      s(1,2,300,5,15,300)
      # 623
      
  • 带*号的形参只能有一个,可以和其它参数配合使用

    • def fn2(a,b,*c):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn2(1, 2, 3, 4, 5)
      # a = 1
      # b = 2
      # c = (3, 4, 5)
      
      def fn2(a,*b,c):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn2(1,2,3,4,5)
      # TypeError: fn2() missing 1 required keyword-only argument: 'c'
      
      def fn2(a,*b,c):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn2(1,2,3,4,c=5)
      # a = 1
      # b = (2, 3, 4)
      # c = 5
      
      def fn2(*a,b,c):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn2(1,2,3,b=4,c=5)
      # a = (1, 2, 3)
      # b = 4
      # c = 5
      
  • *形参只能接受位置参数,不能接受关键字参数

    • def fn3(*b):
          print('b =',b)
      fn3(b=1,d=2,c=3)
      # TypeError: fn3() got an unexpected keyword argument 'b'
      
  • **形参可以接收其他的关键字参数,它会将这些参数统一保存到字典中。字典的key就是参数的名字,value就是参数的值

    • def fn3(**b):
          print('b =',b)
      fn3(b=1,d=2,c=3)
      b = {'b': 1, 'd': 2, 'c': 3}
      
  • **形参只有一个,并且必须写在所有参数的后面

    • def fn3(b,c,**a):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      fn3(b=1,d=2,c=3,e=50,f=80)
      # a = {'d': 2, 'e': 50, 'f': 80}
      # b = 1
      # c = 3
      
4. 参数的解包
  • 传递实参时,可以在序列类型的参数前添加星号,这样它会自动将序列中元素依次作为参数传递

  • 要求序列中的元素的个数必须和形参的个数一致

    • def fn3(a,b,c):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      t = (1,2,3,4)
      fn3(t[0],t[1],t[2])
      # a = 1
      # b = 2
      # c = 3
      
      def fn3(a,b,c):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      t = (1,2,3,4)
      fn3(*t)
      # TypeError: fn3() takes 3 positional arguments but 4 were given
      
      def fn3(a,b,c):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      t = (2,3,4)
      fn3(*t)
      # a = 2
      # b = 3
      # c = 4
      
      def fn3(a,b,c):
          print('a =',a)
          print('b =',b)
          print('c =',c)
      d = {'a':1,'b':2,'c':3}
      fn3(**d)
      # a = 1
      # b = 2
      # c = 3
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值