What do @ and-> represent when def defines a function?

when reading other people"s source code, he found that he used a lot of this way to define functions:

@property
def attrs(self) -> _Attrs:
    pass

I wonder what @ property and -> _ Attrs: are for? Where are the relevant instructions?

Thank you.

Mar.11,2021

Google.

the official document python.org/3/reference/compound_stmts.html-sharpfunction-definitions" rel=" nofollow noreferrer "> function definition is mentioned in this section

.

by the way, post the blog post found:
Python function comment: strange:-> symbol
decorator


< H2 > about @ property < / H2 >

this is a python object-oriented programming problem, which is relatively simple:

@ property is a decorator that enables a class to make a method called by a property.

for example, in the python class, I want to access and set private variables in a way similar to CPP, such as

class Student(object):

    def get_score(self):
         return self._score

    def set_score(self, value):
        if not isinstance(value, int):
            raise ValueError('score must be an integer!')
        if value < 0 or value > 100:
            raise ValueError('score must between 0 ~ 100!')
        self._score = value


s = Student()
s.set_score(60) -sharp ok!
s.get_score()

however, this seems a bit troublesome. In fact, python can get and set variables (non-private variables) of a class directly through the following ways:

class Student(object):
    pass

s = Student()
s.score = 90
print(s.score) -sharp 90

doesn't this look easy? But there is also a danger that the type of variable assignment to the class is uncertain, and it is impossible to check restrictions on the type of variable assignment, such as integers, strings, boolean variables, and so on. If you want to get a value and assign a value in this way, you can achieve it through @ property :

.
class Student(object):
    @property
    def get_score(self):
         return self._score
    @property
    def set_score(self, value):
        if not isinstance(value, int):
            raise ValueError('score must be an integer!')
        if value < 0 or value > 100:
            raise ValueError('score must between 0 ~ 100!')
        self._score = value

s = Student()
s.score = 90
print(s.score) -sharp 90
s.score = '100' -sharp

reference: Liao Xuefeng's python tutorial-using @ property

< H2 > about -> _ Attrs < / H2 >

-> often appears after the function name defined by the python function, adding metadata to the function, describing the return type of the function, which is convenient for developers to use. For example:

def add(x, y) -> int:
  return x+y

here, the metadata indicates that the return value of the function is of type int.
as for the code problem, -> _ Attr indicates that the function returns a private variable of an externally accessible class.

reference: python3-cookbook.readthedocs.io/zh_CN/latest/c07/p03_attach_informatinal_matadata_to_function_arguments.html" rel=" nofollow noreferrer "> add meta information to function parameters


-> third-party database mypy, wants ladders:
http://mypy-lang.org

the variable type used to tell python vm the number of (x) and the returned type, python can be mixed with
dynamic and static typing

the following is part of the official content:

Migrate existing code to static typing, a function at a time. You can freely mix static and dynamic typing within a program, within a module or within an expression. No need to give up dynamic typing-use static typing when it makes sense. Often just adding function signatures gives you statically typed code. Mypy can infer the types of other variables.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bee176-30915.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bee176-30915.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?