Note that there are some explanatory texts on larger screens.

plurals
  1. POPython decorators that are part of a base class cannot be used to decorate member functions in inherited classes
    text
    copied!<p>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 (the decorator will access class members hence it will require the self parameter).</p> <pre><code>class SubSystem(object): def UpdateGUI(self, fun): #function decorator def wrapper(*args): self.updateGUIField(*args) return fun(*args) return wrapper 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 ... </code></pre> <p>I've omitted the rest of the implementation. Now this class is a base class for various SubSystems that will inherit from it - some of the inherited classes will need to use the UpdateGUI decorator.</p> <pre><code>class DO(SubSystem): def getport(self, port): """Returns the value of Digital Output port "port".""" pass @SubSystem.UpdateGUI def setport(self, port, value): """Sets the value of Digital Output port "port".""" pass </code></pre> <p>Once again I have omitted the function implementations as they are not relevant. </p> <p>In short the problem is that while I can access the decorator defined in the base class from the inherited class by specifiying it as SubSystem.UpdateGUI, I ultimately get this TypeError when trying to use it: </p> <p><code>unbound method UpdateGUI() must be called with SubSystem instance as first argument (got function instance instead)</code></p> <p>This is because I have no immediately identifiable way of passing the <code>self</code> parameter to the decorator! </p> <p>Is there a way to do this? Or have I reached the limits of the current decorator implementation in Python?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload