tags:

views:

161

answers:

2

Reading some Python (PyQt) code, I came across as follows.

@pyqtSignature("QString")
def on_findLineEdit_textEdited(self, text):
    self.__index = 0
    self.updateUi()

How does this @pyqtSignature work? How Python treat this @?

+2  A: 

It is a syntax for function decorators.

SilentGhost
+5  A: 

It is the decorator syntax, simply it is equivalent to this form:

on_findLineEdit_textEdited = pyqtSignature("Qstring")(on_findLineEdit_textEdited)

Really simple.

A typical decorator takes as the first argument the function that has to be decorated, and perform stuff/adds functionalities to it. A typical example would be:

def echo_fname(f):
    def newfun():
       print f.__name__
       f()
    return newfun

The steps are:

  • define a new function that add functionalities to f
  • return this new function.
pygabriel
No it isn't equivalent to that. It is equivalent to:`on_findLineEdit_textEdited = pyqtSignature("QString")(on_findLineEdit_textEdited)`So in this case `pyqtSignature` is a function that returns a decorator rather than being a decorator itself.
Duncan
Thank you for the point I've corrected it!
pygabriel