繁体   English   中英

Python使用装饰器实现上下文管理器

[英]Python use decorator to implement context manager

我试图用参数制作两个装饰器。 First使用元素x创建一个list ,然后调用func second是简单地通过传递dict中的参数来调用first一个方法。

def first(x=1):
    def wrapped(func):
        l = [x]
        func(l)
        print(l)
    return wrapped

def second(d={'x':10}):
    return first(x=d['x'])

third函数只是修改传入的列表。我想通过简单地调用third()使下面的四个装饰器中的任何一个成为可能。 我应该如何修改我的代码?

##@second
##@second({'x':100})
##@first
##@first(x=10)
def third(l):
    l.append(-1)

third()

例如:

## With @first,
## I am expecting to get [1, -1].
## With @first(x=10),
## I am expecting to get [10, -1].
## With @second,
## I am expecting to get [10, -1].
## With @second({x:100}),
## I am expecting to get [100, -1].

上层代码是我的问题的抽象。 我的真正问题是我想要一个装饰器来为我处理打开和关闭连接,因此我只需要编写代码来处理连接。

连接需要参数,这是first 我希望以另一种方式传递参数,这是second third是我将如何处理连接。 我希望third函数像普通函数一样被调用,并且它还使用装饰器处理打开和关闭连接。 抱歉,如果不能以这种方式使用装饰器,但是我真的想练习使用它。

---更新---

我要实现的基本上是以下内容:

def context_manager(username='user', password='password'):
    conn = OpenConnection()
    func(conn)
    CloseConnection()

def context_manager2(d={'username': 'user', 'password': 'password'}):
    content_manager(username=d['username'], password=d['password'])

# @context_manager
# @context_manager('username', '123456')
# @context_manager2
# @context_manager2(d={'username': 'username', 'password': '123456'})
def execute(conn):
    pass

我想使四个装饰器中的任何一个都可以,并且仍然能够像execute()一样调用execute

看起来您可能只需要了解装饰器是什么。 装饰器是一个接受函数作为其唯一参数的函数,并在其位置返回一个函数。 它们通常采取以下形式:

def decorator(f):
    def wrapped(*args, **kwargs):
        # it's important to accept any arguments to wrapped, thus *args and **kwargs
        # because then you can wrap _any_ function and simply pass its arguments on.
        print("Inside the wrapped function")

        retval = f(*args, **kwargs)  # pass the arguments through to the decorated function

        print("Exiting the wrapped function")

        return retval

    return wrapped

这使您可以执行以下操作:

@decorator
def my_increment(x):
    print("Calculating...")
    return x + 1

# actually equivalent to
def my_increment(x):
    print("Calculating...")
    return x + 1

my_increment = decorator(my_increment)

并预期结果如下:

>>> print(my_increment(3))
Inside the wrapped function
Calculating...
Exiting the wrapped function
4

值得注意的是: my_increment在运行时而不是在调用时成为修饰的函数。 没有装饰器功能就无法调用my_increment


您尝试执行的操作看起来与使用装饰器的外观看起来不一样。 这对我来说就像函数链。

def first(x=1):
    return [x]

def second(d=None):
    if d is None:
        d = {'x':10}  # why do this? https://stackoverflow.com/q/1132941/3058609
    return first(d['x'])

def third(lst):
    return lst + [-1]

并这样称呼:

# With @first,
# I am expecting to get [1, -1].
third(first())  # [1, -1]

# With @first(x=10),
# I am expecting to get [10, -1].
third(first(10))  # [10, -1]

# With @second,
# I am expecting to get [10, -1].
third(second())  # [10, -1]

# With @second({x:100}),
# I am expecting to get [100, -1].
third(second({'x':100}))  # [100, -1]

还要注意,装饰器可以接受参数,但是您正在谈论的是(带我...)一个接受参数的函数,该参数返回一个函数,该函数接受一个函数并返回一个函数。 您只是抽象了一层。 想像:

def decorator_abstracted(msg):
    """Takes a message and returns a decorator"""

    # the below is almost the exact same code as my first example
    def decorator(f):
        def wrapped(*args, **kwargs):
            print(msg)
            retval = f(*args, **kwargs)
            print("exiting " + msg)
            return retval

        return wrapped
    return decorator

现在您的代码可能是

@decorator_abstracted("a decorator around my_increment")
def my_increment(x):
    print('Calculating...')
    return x + 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM