Python之@staticmethod和@classmethod详解

成员方法(实例方法)、静态方法和类方法

1.实例方法:类中最常用的方法是实例方法, 即通过实例作为第一个参数的方法

class Foo:
    def __init__(self,name):#绑定方法
        self.name=name
    def run(self,a): # 实例方法
        print(self.name,a)
h=Foo('haha') # 实例化变量
h.run(99)

运行结果:

haha 99

run()是实例化方法,第一个参数self指的是当前对象,第二个参数是实例化变量

2.静态方法:

class Foo:
    def __init__(self,name):#绑定方法
        self.name=name
    def show():
        print('have time')

h=Foo('haha') # 实例化变量
h.show()

运行结果如下:

Traceback (most recent call last):
  File "D:/python/day3/实例方法.py", line 8, in <module>
    h.show()
TypeError: show() takes 0 positional arguments but 1 was given

Process finished with exit code 1

凡是通过类实例调用的方法默认给方法传入了一个参数,也就是该实例本身。也就是之前咱们写实例方法时,每个方法的第一个参数 self。如果我们不想要传入类的实例化调用的方法默认给方法传入了一个参数,可以使用@staticmethod

class Foo:
    def __init__(self,name):#绑定方法
        self.name=name
    @staticmethod #静态方法
    def show():
        print('have time')

h=Foo('haha') # 实例化变量
h.show()

就可以在不传参情况下得到预期的结果:

have time

静态方法的应用:如果在方法中不需要访问任何实例化方法和属性,纯粹的通过传入参数并返回数据的功能性方法,那么它就适合用静态方法来定义

3.类方法:当我们需要和类直接进行交互,而不需要和实例进行交互时。类方法就是最好的选择
类方法不需要和实例进行交互,自然也就不需要传入实例本身。再则,类方法需要和类进行交互,所以如果有种方法能够在每次调用时都传入类对象就好了。而这个方法就是使用 @classmethod 来装饰我们的方法。如下代码:

class Foo:
    bar = 'nice'
    def __init__(self,name):#绑定方法
        self.name=name
    def print_name(self):#实例方法
        print(self.name)
    @staticmethod #静态方法
    def show():
        print('have time')
    @classmethod
    def get(cls):
        print(cls.bar)

h=Foo('haha') # 实例化变量
h.print_name()
h.show()
h.get()

运行结果如下:

haha
have time
nice
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Python中,装饰器是一种用来修改其他函数功能的函数。它们通过在被修饰函数的定义前加上@符号来实现。常见的装饰器语法糖包括@classmethod@staticmethod、@property、@xxx.setter和@wraps()等。装饰器可以让我们的代码更简短和更Pythonic。举个例子,我们可以使用装饰器来修改一个函数的行为,如在函数执行前后添加一些额外的功能。例如,下面的代码通过一个装饰器来修改say_whee函数的行为: @my_decorator def say_whee(): print("Whee!") say_whee('李', c='18', b='20') 这里,@my_decorator是一个装饰器,它修饰了say_whee函数。当我们调用say_whee函数时,实际上调用的是被装饰后的函数。装饰器可以在函数执行前后执行一些额外的代码,从而实现对函数功能的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python装饰器详解](https://blog.csdn.net/weixin_44992737/article/details/125868592)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python @装饰器](https://blog.csdn.net/qq_43710438/article/details/125678643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值