zoukankan      html  css  js  c++  java
  • 🍖函数对象与嵌套

    一.函数在 Python 中是第一类对象

    函数对象的四种用法

    def func():     # func=函数的内存地址
        print('from func')
    

    1、可以被引用 (被赋值)

    def bar():
        print('from bar')
    
    f=bar
    f()  # from bar
    

    2、可以当做参数传入

    def bar():
        print('from bar')
    
    def wrapper(func): #func=bar
        func()         #bar()
    
    wrapper(bar)       #from bar
    

    3、可以当做函数的返回值

    def bar():
        print('from bar')
    
    def foo(func):  #func=bar
        return func #return 函数 bar 的内存地址
    
    f=foo(bar)      #f = func = bar
    f()             #bar()---> from bar
    

    4、可以当做容器类型的元素

    建一个列表,将函数放进去
    def get():
        print('from get')
    
    def put():
        print('from put')
    
    l=[get,put]
    
    l[0]()  #---> get()---> from get
    l[1]()  #---> put()---> from put
    
    • 模拟购物车示例
    实现登入,注册,转账,提现等功能
    def login():
        print('login')
    
    def register():
        print('register')
    
    def tranfer():
        print('transfer')
    
    def withdraw():
        print('withdraw')
    
    func_dic={
        "1":[login,"登录"],
        "2":[register,"注册"],
        "3":[tranfer,"转账"],
        "4":[withdraw,"提现"]
    }
    
    while True:
        print("0 退出")
        for k in func_dic:
            print(k,func_dic[k][-1])
        choice=input("请输入操作编号: ").strip()
        if choice == "0":
            break
    
        if choice in func_dic:
            func_dic[choice][0]()
        else:
            print("输入的操作不存在")
    

    二.函数嵌套

    1.函数嵌套定义

    • 在函数内定义其他函数
    def f1():
        print('from f1')
        def f2():
            print('from f2')
        f2()
    f1()
    
    计算圆的周长和面积
    from math import pi         #导入 math 模块
    
    def circle(radius,mode=0):
        def perimiter(radius):  #计算周长函数
            return 2 * pi * radius
    
        def area(radius):       #计算面积函数
            return pi * (radius ** 2)
    
        if mode == 0:           #选 0 则计算周长
            return perimiter(radius)
        elif mode == 1:         #选 1 则计算面积
            return area(radius)
    
    res=circle(10,mode=1)       #传入半径 10 ,选择 1 
    print(res)                  #314......
    

    2.函数嵌套调用

    • 在调用一个函数的过程中又调用其他函数的组合
    比较两个值的大小
    def max2(x,y):
        if x > y:
            return x
        else:
            return y
    
    比较四个值的大小
    def max4(a,b,c,d):
        res1=max2(a,b)
        res2=max2(res1,c)
        res3=max2(res2,d)
        return res3
    
    res=max4(1,2,3,4)
    print(res)  # 4
    
  • 相关阅读:
    Delphi StrUtils-字符串函数RightStr、MidStr、LeftStr
    Delphi 错误:Could not convert variant to type(Null) into type (String)
    Delphi Variants-VarIsEmpty、VarIsNull 判断Variant变量是否为空、是否包含Null值
    Python使用openpyxl读写excel文件
    CentOS7设置为局域网固定ip
    Linux下ps aux命令中STAT的参数含义(转)
    Python生成8位随机字符串的方法分析
    php源码加密方法详解
    普通程序员 与 大牛 的区别 ???
    开始记录前端学习过程中的点点滴滴
  • 原文地址:https://www.cnblogs.com/songhaixing/p/14047151.html
Copyright © 2011-2022 走看看