开发者

What's the meaning of '@' in python code?

开发者 https://www.devze.com 2023-01-05 23:34 出处:网络
Reading some Python (PyQt开发者_运维问答) code, I came across as follows. @pyqtSignature(\"QString\")

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 @?


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.


It is a syntax for function decorators.

0

精彩评论

暂无评论...
验证码 换一张
取 消