python装饰器的使用简介 以及 常用的两种装饰器介绍

python装饰器粗解

一、装饰器的定义

简而言之,本质还是一个函数。但其主要的作用就是在不更改你想要编辑的函数的代码的情况下,为其添加新的功能。
常见的使用主要是:函数性能测试,插入日志等

装饰器的使用

  • 未使用装饰器时
def test_1():
    time.sleep(1)
    print('this is test_1')

def test_2():
    time.sleep(4)
    print('this is test_2')

if __name__ == '__main__':
    test_1()
    test_2()
# 输出结果为
# this is test_1
# this is test_2
  • 使用了装饰器之后 cost_time为当前定义的装饰器函数
import time


def cost_time(func):
    def inner():
        t_start=time.time()
        func()
        t_end=time.time()
        print(f'{func.__name__} cost time :{str(t_end-t_start)}s')
    return inner
@cost_time
def test_1():
    time.sleep(1)
    print('this is test_1')

@cost_time
def test_2():
    time.sleep(4)
    print('this is test_2')

if __name__ == '__main__':
    test_1()
    test_2()
# 输出结果为
#  this is test_1
# test_1 cost time :1.0011262893676758s
# this is test_2
# test_2 cost time :4.000372886657715s

二、 常见的装饰器用法@staticmethod 和@classmethod

使用@staticmethod 和@classmethod可以在不需要实例化类时使用某个类的方法 有利于组织代码 有利于命名空间的整洁

  • 未使用装饰器时的代码如下,本来是同一类的函数,但由于可以随意的放置,导致代码混乱不整洁
import time

def time_sleep_1():
    time.sleep(1)
    print('sleep time 1s')

def print_1():
    print('print 1')

def time_sleep_2():
    time.sleep(2)
    print('sleep time 2s')

def print_2():
    print('print 2')

def time_sleep_3():
    time.sleep(3)
    print('sleep time 3s')

if __name__ == '__main__':
    time_sleep_1()
    time_sleep_2()
    time_sleep_3()
    print_1()
    print_2()
# 输出结果
# sleep time 1s
# sleep time 2s
# sleep time 3s
# print 1
# print 2
  • 使用@staticmethod,这样会使代码会更加的整洁。
import time

class time_sleep(object):

    @staticmethod
    def time_sleep_1():
        time.sleep(1)
        print('sleep time 1s')

    @staticmethod
    def time_sleep_2():
        time.sleep(2)
        print('sleep time 2s')

    @staticmethod
    def time_sleep_3():
        time.sleep(3)
        print('sleep time 3s')

class print_info(object):

    @staticmethod
    def print_1():
        print('print 1')
    @staticmethod
    def print_2():
        print('print 2')



if __name__ == '__main__':
    time_sleep.time_sleep_1()
    time_sleep.time_sleep_2()
    time_sleep.time_sleep_3()
    print_info.print_1()
    print_info.print_2()
# 输出结果
# sleep time 1s
# sleep time 2s
# sleep time 3s
# print 1
# print 2
  • 使用@classmethod ,好处是不论是从实例调用还是从类调用,都用第一个参数把类传递过来,如果不使用@classmethod 则类调用就会报错
import time

class time_sleep(object):

    @staticmethod
    def time_sleep_1():
        time.sleep(1)
        print('sleep time 1s')

    @staticmethod
    def time_sleep_2():
        time.sleep(2)
        print('sleep time 2s')

    @staticmethod
    def time_sleep_3():
        time.sleep(3)
        print('sleep time 3s')

    @classmethod
    def run_time(cls):
        cls.time_sleep_1()
        cls.time_sleep_2()
        cls.time_sleep_3()

class print_info(object):

    @staticmethod
    def print_1():
        print('print 1')
    @staticmethod
    def print_2():
        print('print 2')
    @classmethod
    def run_print(cls):
        cls.print_1()
        cls.print_2()


if __name__ == '__main__':
    time_sleep.run_time()
    print_info.run_print()
	# 或者这样也可以
	t1=time_sleep()
    t1.run_time()
    p1=print_info()
    p1.run_print()
# 输出结果
# sleep time 1s
# sleep time 2s
# sleep time 3s
# print 1
# print 2

# sleep time 1s
# sleep time 2s
# sleep time 3s
# print 1
# print 2

如果不使用@classmethod 进行修饰,那么下列方法使用就会报错

print_info.run_print()

报错如下

    print_info.run_print()
TypeError: run_print() missing 1 required positional argument: 'self'

总结:装饰器是一个很好用的函数,会使你的代码可读性更高,也会更加的简洁明了,但需要注意的是,不是所有的方法你都加装饰器,简单不复用的代码,你添加了装饰器反而增加了代码的冗余程度以及可读的成本

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值