What does the “at” (@) symbol do in Python?

前端 未结 12 955
萌比男神i
萌比男神i 2020-11-22 01:57

I\'m looking at some Python code which used the @ symbol, but I have no idea what it does. I also do not know what to search for as searching Python docs or Goo

12条回答
  •  醉话见心
    2020-11-22 02:45

    This code snippet:

    def decorator(func):
       return func
    
    @decorator
    def some_func():
        pass
    

    Is equivalent to this code:

    def decorator(func):
        return func
    
    def some_func():
        pass
    
    some_func = decorator(some_func)
    

    In the definition of a decorator you can add some modified things that wouldn't be returned by a function normally.

提交回复
热议问题