Today’s learning summary-----flask source code analysis and meaning

1. Usage of : sign in Python

names: t.Iterable[str  None] = (None.)

This line of code is a way of writing type annotation (TypeAnnotation) in Python. In this line of code, names is a variable, which is annotated as type Iterable[strNone]. This means that names is an iterable object whose elements are of type str or None.
Iterable[str None] This type annotation uses a special way of writing, indicating that na can be an iterable object composed of str and None. The messtr None in square brackets indicates that there is a union type relationship between str and None, that is, the elements in names can be str or None. The final = (None,) means that names are assigned an initial value (None,), which is a tuple containing a single element None.
Through type annotations, the readability and maintainability of the code can be increased. Type checking can be performed in static type checking tools (such as mypy) to help find potential problems in the code.

2. What is a coroutine?

Coroutines are also called micro-threads. They are executed in a thread. You can terminate at any time when executing a function and are controlled by the program itself. The execution efficiency is extremely high. Compared with multi-threads, there is no overhead of switching threads and no multi-thread lock mechanism.

3. Understanding asynchronous programming

Asynchronous programming: can be seen as similar to county but does not involve system scheduling, that is, asynchronous programs can handle problems concurrently when the context of the asynchronous program is not switched internally through the system scheduler.

4. asyncio asynchronous coroutine

asyncio, Asynchronous I/O, is a python package used to handle concurrent events. It is the basis of many python asynchronous architectures and is used to handle problems with high concurrent network requests.

# 普通函数
def function():
    return 1
 
 
# 由async做前缀的普通函数变成了异步函数
async def asynchronous():
    return 1
 
 
# 而异步函数不同于普通函数不可能被直接调用
async def asynchronous():
    return 1
 
print(asynchronous())

5. Indefinite length parameters

 *args accepts a single occurrence of the argument and stores it as a tuple.

**kwargs receives parameters in the form of key-value pairs and stores them in a dictionary after receiving them.

6.Python walrus operator definition

A variable name followed by an expression or a value, this is a new assignment operator.

# 基础写法
x = 5
if x < 10:
    print("hello fancy!")
 
 
# 海象运算符写法
if (x:=5) < 10:
    print("hello fancy!")

7. python keyword parameters

Python keyword arguments refer to using the names of formal parameters to determine the actual input parameters when calling a function. Python keyword parameters can avoid the trouble of remembering the parameter location, making function calling and parameter passing more flexible and convenient.

def info(name, age, gender):
    print('姓名:', name, ': 年龄:', age, ': 性别:', gender)
 
 
info(name='阿姐', 18, gender='男')
 
### 错误 ###
  File "E:\p12\cope\first_learning\referral.py", line 20
    info(name='阿姐', 18, gender='男')
                                        ^
SyntaxError: positional argument follows keyword argument
############

8.await keyword

Await is a keyword that can only be used in coroutine functions and is used to suspend the current coroutine when encountering IO operations. During the process of suspending the current coroutine, the event loop can execute other coroutines. After the current coroutine IO processing is completed, the executed code can be switched again.

9. Introduction and use of @property in python


Python's @property is a decorator in Python, which is used to decorate methods. You can use the @propery decorator to create read-only properties. The @propert decorator will convert the method into a read-only property with the same name, which can be used with the defined property to prevent the property from being modified.

class DataSet(object):
    @property
    def method_with_property(self):
        return 15
    
    def method_without_property(self):
        return 15
 
ls = DatatSet()
print(ls.method_with_property) # 加了@property后,可以用调用属性的形式调用方法,后面不需要加()
print(ls.method_without_property()) # 没有加@property,必须使用正常的调用方法的形式,即在后面加()

10. python default parameters

Python default parameters refer to directly setting a default value for the formal parameters when defining a function.

def info(name='null', age, gender='男'):
    print('姓名:', name, ': 年龄:', age, ': 性别:', gender)
 
info()
 
### 错误 ###
File "E:\p12\cope\first_learning\referral.py", line 15
    def info(name='null', age, gender='男'):
                               ^
SyntaxError: non-default argument follows default argument
###########

Guess you like

Origin blog.csdn.net/weixin_72059344/article/details/131947970