PrefaceIn the previous few articles, several core components of Python, including object protocol, number protocol, comparison protocol, conversion protocol, container protocol, iteration protocol, and attribute protocol, were briefly introduced with example codes. This article introduces the Function Protocol and the Context Manager Protocol. Function ProtocolWe know that Python is a dynamic language. The interpreter reads the code and interprets it. This involves the interpretation and execution of functions (in the context of a class, the functions of the object to which they belong are called methods), so certain protocol rules must be followed, otherwise it will be a mess. In an object, an object can simulate a function by providing a __call__() method. Assuming an object x provides this method, it can be called like a function. In other words, x(__arg1, arg2, ...) actually calls x.__call__ (__arg1, arg2, ...). A simple example is as follows: class XxxCalling : In fact, the last line of the above code can also be written as: xc.__call__('祖龙', '中国'), the result is exactly the same. For callable objects, in fact, "name()" can be understood as an abbreviation of "name.__call__()". Many built-in types support function calls. For example, types implement __call__() to create new instances. Bound methods implement __call__() to pass the self argument to instance methods. Library functions like functools.partial() also create objects that emulate functions. Context Management ProtocolThe so-called context management protocol here mainly refers to a block of code in a specific scope. Python uses the with statement to implement it, that is, the with statement allows a series of statements to be executed under the control of an instance called a context manager. The general syntax is as follows: with context [ as var ] : Implementing such a context protocol requires supporting the following specific methods:
The __enter__() method is called when a with statement is executed. The value returned by this method is placed into the variable specified with the optional as var specifier. The __exit__() method is called when control-flow leaves the block of statements associated with the with statement. As arguments, __exit__() receives the current exception type, value, and, if an exception was raised, a traceback. If no error was handled, all three values are set to None. The __exit__() method should return True or False to indicate whether the raised exception was handled. If it returns True, any pending exception is cleared and program execution continues normally with the first statement after the with block. The primary purpose of the context management interface is to allow simplified resource control over objects that involve system state, such as open files, network connections, and locks. By implementing this interface, an object can safely clean up resources when execution leaves the context in which it is used. summaryThis is all I have to say for this article. I have briefly introduced the implementation of the function protocol (directly calling objects as functions) and the implementation of the context protocol. |
<<: Five ways 5G will change manufacturing
>>: How will 6G change the workplace?
Even with all the coffee or energy drinks in the ...
Let me first state the following conclusion. 1 Sp...
Network as a Service (NaaS) is increasingly popul...
Recently, China Unicom officially announced its 2...
On the one hand, the higher frequency band of 5G ...
In the ever-evolving field of education, technolo...
2020 is a year of rapid development of 5G. The sa...
On May 12, more than 75,000 computer virus attack...
RackNerd has launched its 2021 New Year promotion...
[[430598]] The shopping festival is here. If you ...
As a representative technology, 5G has become the...
The last time I shared information about Lisa Hos...
Tribe has shared information about DiyVM many tim...
2019 has come to an end, and the annual flagships...
5G is a leading technology in the new generation ...