代码之家  ›  专栏  ›  技术社区  ›  Vini.g.fer

Python:为类方法创建带参数的装饰器

  •  0
  • Vini.g.fer  · 技术社区  · 4 年前

    试图为类中的几个方法创建一个装饰器。装饰器在没有参数的情况下按预期工作:

    class OtherMenu:                                                                                                        
        current_page = 'algo'                                                                                               
                              
                                                                                       
    class MenuSidebarWebScreen(OtherMenu):                                                                                  
                                                                                                                         
        def decorator(foo):                                                                                                 
            def magic(self):                                                                                                
                import ipdb                                                                                                 
                ipdb.set_trace()                                                                                            
                print("start magic")                                                                                        
                foo(self)                                                                                                   
                print("end magic")                                                                                          
            return magic                                                                                                    
                                                                                                                         
        @decorator                                                                                                          
        def go_to_home_page(self):                                                                                          
            if self.current_page == "Home":                                                                                 
                print(f'Already in the {self.current_page}, skipping menu interaction.')                                    
                return                                                                                                      
            # Do awesome stuff over here
            self.current_page = "Home"
    
    >>> from features.steps.web_ui.menu.menu_sidebar_web_screen import MenuSidebarWebScreen
    >>> menu = MenuSidebarWebScreen()
    >>> menu.go_to_home_page()
    ipdb> foo
    <function MenuSidebarWebScreen.go_to_home_page at 0x10dbd1280>
    ipdb> self
    <features.steps.web_ui.menu.menu_sidebar_web_screen.MenuSidebarWebScreen object at 0x10c85e280>
    ipdb>
    

    但是当我向decorator添加参数时,如下所示

            @decorator("Something")  <----------- Added a parameter to the decorator
            def go_to_home_page(self):                                                                                          
                if self.current_page == "Home":                                                                                 
    

    行为发生变化。首先,在导入类时立即调用decorator方法(而不是调用类方法)。此外,pdb本身的值更改为:

    >>> from features.steps.web_ui.menu.menu_sidebar_web_screen import MenuSidebarWebScreen
    ipdb> self
    <function MenuSidebarWebScreen.go_to_home_page at 0x10a07c310>
    ipdb> foo
    'Something'
    

    在decorator中使用类的正确方法是什么?我希望将代码的这些部分用于装饰器本身:

                if self.current_page == "Home":                                                                                 
                    print(f'Already in the {self.current_page}, skipping menu interaction.')                                    
                    return                                                                                                      
                ... ------> Everything else, that will remain on the go_to_home_page method
                self.current_page = "Home"
    
    0 回复  |  直到 4 年前
    推荐文章