zoukankan      html  css  js  c++  java
  • day01-函数及函数式编程(位置参数及关键字参数)

    1.无参数

    #无参数
    def func1():
        print('in func1')
        return 99
    def func2():
        print('in func2')
    y1=func1()
    y2=func2()
    print(y1,y2)

    2.返回值: 

    #返回值
    def myprint():
        import time
        with open('myprint.txt','a+') as f:
            f.write(time.asctime(time.localtime()))
            f.write('
    ')
    def print1():
        print('this is in print1')
        myprint()
    def print2():
        print('this is in print2')
        myprint()
        return print1()
    def print3():
        print('this is in print3')
        myprint()
        return print2()
    print3()

    输出:

    this is in print3
    this is in print2
    this is in print1

    3.位置参数与关键字参数

    定义函数:

    def test(x,y):
        print('形参x=%s'%x)
        print('形参y=%s'%y)
    #关键字参数调用
    test(y=2,x=1)
    x=10
    y=20
    test(y=x,x=y)
    test(x=x,y=y)

    输出:

    形参x=1
    形参y=2
    形参x=20
    形参y=10
    形参x=10
    形参y=20

    原由:区分形参和实参

    4.位置参数调用:

    #位置参数调用
    test(10,20)

    5.混合调用:

    test(x=10,2)
      File "E:/OldBoy/day01/函数与函数式编程.py", line 47
        test(x=10,2)
                 ^
    SyntaxError: positional argument follows keyword argument

    说明关键字参数不能在位置参数前面。

    test(2,x=10)

    File "E:/OldBoy/day01/函数与函数式编程.py", line 49, in <module>
    test(2,x=10)
    TypeError: test() got multiple values for argument 'x'

    说明位置参数和关键字参数在一起时候,都默认为位置参数。

    test(2,y=10)

    输出:

    形参x=2
    形参y=10
  • 相关阅读:
    Spring Boot 缓存技术:Spring Boot
    Java基础之Iterable接口
    使用sqlyog连接 Mysql 出现1251错误
    IDEA更改主题插件——Material Theme UI详解
    免安装版的Mysql
    使用Nexus搭建Maven私服
    Spring之注解注入bean
    Idea springboot 配置热部署
    Spring Boot 异常处理与单元测试
    Ubuntu20.04在线安装VMware-Tools
  • 原文地址:https://www.cnblogs.com/Franklin-Kite/p/7455720.html
Copyright © 2011-2022 走看看