Python 问题:如何从注入的方法引用在decorator中创建的对象/变量?

Python 问题:如何从注入的方法引用在decorator中创建的对象/变量?,python,reference,decorator,Python,Reference,Decorator,我在decorator中创建的对象的可用性方面遇到了一个问题,并且在test\u案例方法中需要这些对象。我的代码如下: def execute_results_navigation(test_case): def wrapper(self,*args,**kwargs): result=Result() pagination=Pagination() results_page_index=1 while results_pa

我在decorator中创建的对象的可用性方面遇到了一个问题,并且在
test\u案例
方法中需要这些对象。我的代码如下:

def execute_results_navigation(test_case):
    def wrapper(self,*args,**kwargs):
        result=Result()
        pagination=Pagination()
        results_page_index=1
        while results_page_index<=pagination.get_pages_number():

            for results_object_index in range(results.get_objects_number_per_single_page()):
                test_case(self,*args,**kwargs)

                pagination.set_active_page_number(results_page_index)
            results_page_index+=1

    return wrapper

测试用例
方法无法访问
结果
分页
对象和
结果对象索引
变量。调用decorator时,所有对象都已初始化。这个方法可能有问题,但我认为这些实例存在于
包装器
方法中,对它们的访问应该不会引起问题。

您将无法访问测试用例中包装器中定义的局部变量


看起来搜索结果的test\u check\u availability\u是一种实例方法,所以解决问题的一种方法是将这些变量分配给“self”的属性

您是否看到运行时问题,或者只是担心“编译错误”——这是一个运行时问题。当我引用在decorator中创建的对象时,就在原地。
@execute_results_navigation
def test_check_availability_of_search_results(self):
    """
    test case 2.22
    """
    offer=Offer()

    result.select_hotel(results_caller["button"],results_object_index)
    offer_price=offer.get_offer_object_details().price
    offer.verify_offer_availability(offer_price)
    offer.back_to_search_results()