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

前端 未结 12 950
萌比男神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:27

    @ symbol is also used to access variables inside a plydata / pandas dataframe query, pandas.DataFrame.query. Example:

    df = pandas.DataFrame({'foo': [1,2,15,17]})
    y = 10
    df >> query('foo > @y') # plydata
    df.query('foo > @y') # pandas
    
    0 讨论(0)
  • 2020-11-22 02:32

    Example

    class Pizza(object):
        def __init__(self):
            self.toppings = []
    
        def __call__(self, topping):
            # When using '@instance_of_pizza' before a function definition
            # the function gets passed onto 'topping'.
            self.toppings.append(topping())
    
        def __repr__(self):
            return str(self.toppings)
    
    pizza = Pizza()
    
    @pizza
    def cheese():
        return 'cheese'
    @pizza
    def sauce():
        return 'sauce'
    
    print pizza
    # ['cheese', 'sauce']
    

    This shows that the function/method/class you're defining after a decorator is just basically passed on as an argument to the function/method immediately after the @ sign.

    First sighting

    The microframework Flask introduces decorators from the very beginning in the following format:

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello World!"
    

    This in turn translates to:

    rule      = "/"
    view_func = hello
    # They go as arguments here in 'flask/app.py'
    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
        pass
    

    Realizing this finally allowed me to feel at peace with Flask.

    0 讨论(0)
  • 2020-11-22 02:36

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

    @ symbol is a syntactic sugar python provides to utilize decorator,
    to paraphrase the question, It's exactly about what does decorator do in Python?

    Put it simple decorator allow you to modify a given function's definition without touch its innermost (it's closure).
    It's the most case when you import wonderful package from third party. You can visualize it, you can use it, but you cannot touch its innermost and its heart.

    Here is a quick example,
    suppose I define a read_a_book function on Ipython

    In [9]: def read_a_book():
       ...:     return "I am reading the book: "
       ...: 
    In [10]: read_a_book()
    Out[10]: 'I am reading the book: '
    

    You see, I forgot to add a name to it.
    How to solve such a problem? Of course, I could re-define the function as:

    def read_a_book():
        return "I am reading the book: 'Python Cookbook'"
    

    Nevertheless, what if I'm not allowed to manipulate the original function, or if there are thousands of such function to be handled.

    Solve the problem by thinking different and define a new_function

    def add_a_book(func):
        def wrapper():
            return func() + "Python Cookbook"
        return wrapper
    

    Then employ it.

    In [14]: read_a_book = add_a_book(read_a_book)
    In [15]: read_a_book()
    Out[15]: 'I am reading the book: Python Cookbook'
    

    Tada, you see, I amended read_a_book without touching it inner closure. Nothing stops me equipped with decorator.

    What's about @

    @add_a_book
    def read_a_book():
        return "I am reading the book: "
    In [17]: read_a_book()
    Out[17]: 'I am reading the book: Python Cookbook'
    

    @add_a_book is a fancy and handy way to say read_a_book = add_a_book(read_a_book), it's a syntactic sugar, there's nothing more fancier about it.

    0 讨论(0)
  • 2020-11-22 02:36

    If you are referring to some code in a python notebook which is using Numpy library, then @ operator means Matrix Multiplication. For example:

    import numpy as np
    def forward(xi, W1, b1, W2, b2):
        z1 = W1 @ xi + b1
        a1 = sigma(z1)
        z2 = W2 @ a1 + b2
        return z2, a1
    
    0 讨论(0)
  • 2020-11-22 02:37

    Decorators were added in Python to make function and method wrapping (a function that receives a function and returns an enhanced one) easier to read and understand. The original use case was to be able to define the methods as class methods or static methods on the head of their definition. Without the decorator syntax, it would require a rather sparse and repetitive definition:

    class WithoutDecorators:
    def some_static_method():
        print("this is static method")
    some_static_method = staticmethod(some_static_method)
    
    def some_class_method(cls):
        print("this is class method")
    some_class_method = classmethod(some_class_method)
    

    If the decorator syntax is used for the same purpose, the code is shorter and easier to understand:

    class WithDecorators:
        @staticmethod
        def some_static_method():
            print("this is static method")
    
        @classmethod
        def some_class_method(cls):
            print("this is class method")
    

    General syntax and possible implementations

    The decorator is generally a named object ( lambda expressions are not allowed) that accepts a single argument when called (it will be the decorated function) and returns another callable object. "Callable" is used here instead of "function" with premeditation. While decorators are often discussed in the scope of methods and functions, they are not limited to them. In fact, anything that is callable (any object that implements the _call__ method is considered callable), can be used as a decorator and often objects returned by them are not simple functions but more instances of more complex classes implementing their own __call_ method.

    The decorator syntax is simply only a syntactic sugar. Consider the following decorator usage:

    @some_decorator
    def decorated_function():
        pass
    

    This can always be replaced by an explicit decorator call and function reassignment:

    def decorated_function():
        pass
    decorated_function = some_decorator(decorated_function)
    

    However, the latter is less readable and also very hard to understand if multiple decorators are used on a single function. Decorators can be used in multiple different ways as shown below:

    As a function

    There are many ways to write custom decorators, but the simplest way is to write a function that returns a subfunction that wraps the original function call.

    The generic patterns is as follows:

    def mydecorator(function):
        def wrapped(*args, **kwargs):
            # do some stuff before the original
            # function gets called
            result = function(*args, **kwargs)
            # do some stuff after function call and
            # return the result
            return result
        # return wrapper as a decorated function
        return wrapped
    

    As a class

    While decorators almost always can be implemented using functions, there are some situations when using user-defined classes is a better option. This is often true when the decorator needs complex parametrization or it depends on a specific state.

    The generic pattern for a nonparametrized decorator as a class is as follows:

    class DecoratorAsClass:
        def __init__(self, function):
            self.function = function
    
        def __call__(self, *args, **kwargs):
            # do some stuff before the original
            # function gets called
            result = self.function(*args, **kwargs)
            # do some stuff after function call and
            # return the result
            return result
    

    Parametrizing decorators

    In real code, there is often a need to use decorators that can be parametrized. When the function is used as a decorator, then the solution is simple—a second level of wrapping has to be used. Here is a simple example of the decorator that repeats the execution of a decorated function the specified number of times every time it is called:

    def repeat(number=3):
    """Cause decorated function to be repeated a number of times.
    
    Last value of original function call is returned as a result
    :param number: number of repetitions, 3 if not specified
    """
    def actual_decorator(function):
        def wrapper(*args, **kwargs):
            result = None
            for _ in range(number):
                result = function(*args, **kwargs)
            return result
        return wrapper
    return actual_decorator
    

    The decorator defined this way can accept parameters:

    >>> @repeat(2)
    ... def foo():
    ...     print("foo")
    ...
    >>> foo()
    foo
    foo
    

    Note that even if the parametrized decorator has default values for its arguments, the parentheses after its name is required. The correct way to use the preceding decorator with default arguments is as follows:

    >>> @repeat()
    ... def bar():
    ...     print("bar")
    ...
    >>> bar()
    bar
    bar
    bar
    

    Finally lets see decorators with Properties.

    Properties

    The properties provide a built-in descriptor type that knows how to link an attribute to a set of methods. A property takes four optional arguments: fget , fset , fdel , and doc . The last one can be provided to define a docstring that is linked to the attribute as if it were a method. Here is an example of a Rectangle class that can be controlled either by direct access to attributes that store two corner points or by using the width , and height properties:

    class Rectangle:
        def __init__(self, x1, y1, x2, y2):
            self.x1, self.y1 = x1, y1
            self.x2, self.y2 = x2, y2
    
        def _width_get(self):
            return self.x2 - self.x1
    
        def _width_set(self, value):
            self.x2 = self.x1 + value
    
        def _height_get(self):
            return self.y2 - self.y1
    
        def _height_set(self, value):
            self.y2 = self.y1 + value
    
        width = property(
            _width_get, _width_set,
            doc="rectangle width measured from left"
        )
        height = property(
            _height_get, _height_set,
            doc="rectangle height measured from top"
        )
    
        def __repr__(self):
            return "{}({}, {}, {}, {})".format(
                self.__class__.__name__,
                self.x1, self.y1, self.x2, self.y2
        )
    

    The best syntax for creating properties is using property as a decorator. This will reduce the number of method signatures inside of the class and make code more readable and maintainable. With decorators the above class becomes:

    class Rectangle:
        def __init__(self, x1, y1, x2, y2):
            self.x1, self.y1 = x1, y1
            self.x2, self.y2 = x2, y2
    
        @property
        def width(self):
            """rectangle height measured from top"""
            return self.x2 - self.x1
    
        @width.setter
        def width(self, value):
            self.x2 = self.x1 + value
    
        @property
        def height(self):
            """rectangle height measured from top"""
            return self.y2 - self.y1
    
        @height.setter
        def height(self, value):
            self.y2 = self.y1 + value
    
    0 讨论(0)
  • 2020-11-22 02:38

    An @ symbol at the beginning of a line is used for class, function and method decorators.

    Read more here:

    PEP 318: Decorators

    Python Decorators

    The most common Python decorators you'll run into are:

    @property

    @classmethod

    @staticmethod

    If you see an @ in the middle of a line, that's a different thing, matrix multiplication. Scroll down to see other answers that address that use of @.

    0 讨论(0)
提交回复
热议问题