当前位置: 代码网 > it编程>前端脚本>Python > python类函数的有效调用方式

python类函数的有效调用方式

2024年11月21日 Python 我要评论
一、实验1、准备类的几种方法(1)被@property装饰的属性方法 test_001(2)被@staticmethod装饰的静态方法 test_002(3)被@classmethod装饰的类方法 t

一、实验

1、准备类的几种方法

  • (1)被@property装饰的属性方法 test_001
  • (2)被@staticmethod装饰的静态方法 test_002
  • (3)被@classmethod装饰的类方法 test_003
  • (4)没有任何装饰的普通方法 test_004
  • (5)保护方法 _test_005
  • (6)私有方法 __test_006

2、实验结果见图片

class testclass(object):

    @property
    def test_001(self):  # 第一个传参必须是self
        return 1

    @staticmethod
    def test_002():  # 没有固定传参
        return 2

    @classmethod
    def test_003(cls):  # 第一个传参必须是cls
        return 3

    def test_004(self):  # 第一个传参必须是self
        return 4

    def _test_005(self):  # 第一个传参必须是self
        return 5

    def __test_006(self):  # 第一个传参必须是self
        return 6

def _test_007(): 
    return 7

print(_test_007())  # 值

if __name__ == "__main__":

    # 类没有实例化,函数没有实例化,直接调用函数对象
    print("testclass.test_001:", testclass.test_001)  # 函数对象
    print("testclass.test_002:", testclass.test_002)  # 函数对象
    print("testclass.test_003:", testclass.test_003)  # 函数对象
    print("testclass.test_004:", testclass.test_004)  # 函数对象
    print("testclass._test_005:", testclass._test_005)  # 函数对象
    # print("testclass.__test_006:", testclass.__test_006)  # 直接报错,说明该调用方法不行
    # 类实例化,函数没有实例化,直接调用函数对象
    print("testclass().test_001:", testclass().test_001)  # 值
    print("testclass().test_002:", testclass().test_002)  # 函数对象
    print("testclass().test_003:", testclass().test_003)  # 函数对象
    print("testclass().test_004:", testclass().test_004)  # 函数对象
    print("testclass()._test_005:", testclass()._test_005)  # 函数对象
    # print("testclass().__test_006:", testclass().__test_006)  # 直接报错,说明该调用方法不行
    # 类实例化,函数实例化,再调用函数对象
    # print("testclass().test_001():", testclass().test_001())  # 直接报错,说明该调用方法不行
    print("testclass().test_002():", testclass().test_002())  # 值
    print("testclass().test_003():", testclass().test_003())  # 值
    print("testclass().test_004():", testclass().test_004())  # 值
    print("testclass()._test_005():", testclass()._test_005())  # 值
    # print("testclass().__test_006():", testclass().__test_006())  # 直接报错,说明该调用方法不行
    # 类没有实例化,函数实例化,再调用函数对象
    # print("testclass.test_001:", testclass.test_001())  # 直接报错,说明该调用方法不行
    print("testclass.test_002():", testclass.test_002())  # 值
    print("testclass.test_003():", testclass.test_003())  # 值
    # print("testclass.test_004():", testclass.test_004())  # 直接报错,说明该调用方法不行
    # print("testclass._test_005():", testclass._test_005())  # 直接报错,说明该调用方法不行
    # print("testclass.__test_006():", testclass.__test_006())  # 直接报错,说明该调用方法不行
    # 私有方法调用方式
    print("testclass()._testclass__test_006():", testclass()._testclass__test_006())

二、类的有效调用

  • 1、去掉报错的方式
  • 2、去掉返回对象的方式
  • 3、保留具有返回值的方式
方法类型调用方式
test_001(属性方法)testclass().test_001
test_002(静态方法)

testclass().test_002()

testclass.test_002()

test_003(类方法)

testclass().test_003()

testclass.test_003()

test_004(普通方法)testclass().test_004()
_test_005(保护方法)

testclass()._test_005()

(备注:当不在类内部时,即独立出来成为模块的方法,那么被其他模块导入时,若采用

from moudle import *

的方法,该方法将不被引入,但类内部还是可以的)

__test_006(私有方法)

禁止调用

(备注:可以通过其他方式调用

testclass()._testclass__test_006() )

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com