Python decorators that are part of a base class cannot be used to decorate member functions in inherited classes

前端 未结 4 827
滥情空心
滥情空心 2020-12-28 14:59

Python decorators are fun to use, but I appear to have hit a wall due to the way arguments are passed to decorators. Here I have a decorator defined as part of a base class

4条回答
  •  温柔的废话
    2020-12-28 15:20

    It might be easier to just pull the decorator out of the SubSytem class: (Note that I'm assuming that the self that calls setport is the same self that you wish to use to call updateGUIField.)

    def UpdateGUI(fun): #function decorator
        def wrapper(self,*args):
            self.updateGUIField(*args)
            return fun(self,*args)
        return wrapper
    
    class SubSystem(object):
        def updateGUIField(self, name, value):
            # if name in self.gui:
            #     if type(self.gui[name]) == System.Windows.Controls.CheckBox:
            #         self.gui[name].IsChecked = value #update checkbox on ui 
            #     elif type(self.gui[name]) == System.Windows.Controls.Slider:
            #         self.gui[name].Value = value # update slider on ui 
            print(name,value)
    
    class DO(SubSystem):
        @UpdateGUI
        def setport(self, port, value):
            """Sets the value of Digital Output port "port"."""
            pass
    
    do=DO()
    do.setport('p','v')
    # ('p', 'v')
    

提交回复
热议问题