【问题标题】:how to use **kwargs as parameter for decorator in python如何在python中使用**kwargs作为装饰器的参数
【发布时间】:2021-12-30 17:28:19
【问题描述】:

我想知道我落后的地方。我是 python 装饰器的新手。 我们如何将关键字参数作为参数传递给装饰器?

import time
import math

def deco(x,y,*args,**kwargs): #I want to pass kwarg like="andrew" here 
    def short(func):
        def longer(r,*args,**kwargs):
            print("before func exe")
            begin = time.time()
            func(r)
            end = time.time()
            print("after func exe")
            print("I am ",kwargs['like'])
            print("Total time taken in : ", func.__name__, end - begin)
        return longer
    return short

@deco(3,4,like="andrew")   
def greet(r):
    r = "I will be selelcted"
    print(r)
    # print("I am sorry for demanding")
    
print(greet('r'))

在这里,如果我们尝试为 **kwargs 传递 like="andrew" 则会引发错误。 那怎么传给deco呢?

我也试过喜欢这个。

def deco(x,y,like):
    def short(func):
def deco(x,y,'like'):
    def short(func):

我在上述方法中犯了什么错误?

它抛出错误。 KeyError: '喜欢'

【问题讨论】:

标签: python collections decorator python-decorators keyword-argument


【解决方案1】:

您在错误的范围内访问 kwargs['like']。它应该在short() 范围内。另外,你不需要print()greet()的结果:

def deco(x,y,*args,**kwargs): #I want to pass kwarg like="andrew" here 
    def short(func):
        like = kwargs["like"]
        def longer(r,*args,**kwargs):
            print("before func exe")
            begin = time.time()
            func(r)
            end = time.time()
            print("after func exe")
            print("I am ",like)
            print("Total time taken in : ", func.__name__, end - begin)
        return longer
    return short

@deco(3,4,like="andrew")   
def greet(r):
    r = "I will be selelcted"
    print(r)
    # print("I am sorry for demanding")
    
greet('r')

这给了我:

➜  kwargs python main.py
before func exe
I will be selelcted
after func exe
I am  andrew
Total time taken in :  greet 7.3909759521484375e-06

【讨论】:

  • 我可以知道为什么当它属于 deco 函数时,必须在 short 函数中访问“like”?我们不应该在 deco(x,y,**kw) 中提到关键字“like”吗?那是错的吗?请帮忙。
  • 那也不错。您只是无法在 longer() 函数中访问它,因为此时您已经重新定义了 kwargs
【解决方案2】:

你甚至可以在课堂上使用装饰器。使用另一个函数将参数传递给装饰器。

from pytube import YouTube

class term :
#use another function to pass arguments to decorator
    def show(x,like):
        def check(func): #function to be passed to decorate
            def parameters(self,u,v): #parameters inside function
                print(f"{like} likes no {x} for his jersey")
                func(self,u,v)
                print("Yes done")
            return parameters 
        return check
        
    x = 5
    @show(x,like = "andrew")
    def video1(self,u,v):
        video1 = YouTube(u).streams.first().download(v)
        return video1

u = input("Enter the video URL: ")
v = input("Enter the path: ")

t1 = term()
t1.video1(u,v)
print(t1)

输出:

Enter the video URL: https://www.youtube.com/watch?v=rUWxSEwctFU
Enter the path: E:\
andrew likes no 5 for his jersey
Yes done

【讨论】:

    猜你喜欢
    • 2019-02-06
    • 2016-12-16
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多