• 【Python】函数(function)和方法(method)的区别


    这里先说结论,为了满足心急的小伙伴:methodfunction的最大区别就是参数有无进行绑定


    自定义类Test:

    首先先来一个自定义类:

    class Test:
        def Func_normal(arg):
            print('Func_normal:',arg)
        @staticmethod
        def Func_static(arg):
            print('Func_static:',arg)
        @classmethod
        def Func_class(arg):
            print('Func_class:',arg)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9


    代码样例:

    先来一份代码以及运行结果:

    obj=Test()#Test为本文开头提到的自定义类
    for item in ['Test','obj']:
        print('\n'+('class'if item=='Test' else 'object'))
        for name in ['normal','static','class']:
            print(f'[{name}]',eval(f'{item}.Func_{name}'))
    
    • 1
    • 2
    • 3
    • 4
    • 5

    运行结果-1

    为了更直观地看出差别,这里简单的编写一个表格:

    属性访问[normal][@staticmethod][@classmethod]
    classfunctionfunctionmethod-class
    objectmethod-objectfunctionmethod-class

    可以看出通过类和通过对象访问到的是不完全一致的,而这与本主题有关。
    但是仅仅通过这个还不够直观地表现出它们的差异性,这里再附加一份测试代码以及运行结果:

    obj=Test()#Test为本文开头提到的自定义类
    for item in ['Test','obj']:
        for name in ['normal','static','class']:
            try:
                tx=f'{item}.Func_{name}()'
                print('>>>',tx)
                exec(tx)
            except Exception as e:
                print(e)
        print()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行结果-2

    这里同样贴心地将上面的结果整理成表格便于对比:

    不传参数[normal][@staticmethod][@classmethod]
    class<错误:缺失1参><错误:缺失1参>class
    objectobject<错误:缺失1参>class


    分析:

    在上面的代码样例中得到两张表格,这里再重新把俩表格放在一起以便进行对比,请仔细比对俩表格之间的差异。

    属性访问[normal][@staticmethod][@classmethod]
    classfunctionfunctionmethod-class
    objectmethod-objectfunctionmethod-class
    不传参数[normal][@staticmethod][@classmethod]
    class<错误:缺失1参><错误:缺失1参>class
    objectobject<错误:缺失1参>class

    以下为结论:

    • methodfunction的最大区别就是参数有无进行绑定。
    • 在本例中,method在调用时不需要参数,因为第一个参数已经与特定对象进行了绑定,而function需要传入1参数才能正常调用。
    • @classmethod的作用是将函数的第一个参数绑定为本类(无论是通过类还是类对象进行调用),@staticmethod的作用则是撤去第一个参数的绑定。


    完整代码:

    class Test:
        def Func_normal(arg):
            print('Func_normal:',arg)
        @staticmethod
        def Func_static(arg):
            print('Func_static:',arg)
        @classmethod
        def Func_class(arg):
            print('Func_class:',arg)
    
    obj=Test()
    for item in ['Test','obj']:
        print('\n'+('class'if item=='Test' else 'object'))
        for name in ['normal','static','class']:
            print(f'[{name}]',eval(f'{item}.Func_{name}'))
    
    print('\n\n'+'——'*30+'\n\n')
    
    obj=Test()
    for item in ['Test','obj']:
        for name in ['normal','static','class']:
            try:
                tx=f'{item}.Func_{name}()'
                print('>>>',tx)
                exec(tx)
            except Exception as e:
                print(e)
        print()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    本文发布于CSDN,未经本人同意不得私自转载:https://blog.csdn.net/weixin_44733774/article/details/133509177

  • 相关阅读:
    spark性能调优 | 默认并行度
    【编程题】【Scratch四级】2021.09 小猫钓鱼
    猿创征文|Vue源码分析(Render渲染函数)
    直播出海 | 国内直播间再出爆品,「外卷」全球如何致胜
    RHCE---搭建博客网站
    Linux服务器安装部署最新稳定版本mongoDB社区版- Ubuntu-20.04版本
    CH549/CH548学习笔记5 - SPI主模式
    git(部分)
    【效率提升】maven 转 gradle 实战 | 京东云技术团队
    LeetCode50天刷题计划第二季(Day 32 — 阶乘后的零(13.20-14.00)
  • 原文地址:https://blog.csdn.net/weixin_44733774/article/details/133509177