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?
I received an email from ProfitServer, saying tha...
Hello everyone! I am Xiaomi, a 29-year-old who is...
As technical standards and specifications are det...
With the continuous development of industrial int...
Everyone should be familiar with network slicing....
On April 25, China Mobile General Manager Dong Xi...
After Wi-Fi 6, wireless networks have also ushere...
While MPLS still dominates the WAN market, no ent...
The article shows that the United States is incre...
[[272520]] When users apply for number portabilit...
With the rapid development of wireless networks, ...
inet.WS has released a 25% discount coupon code f...
1. Composition of the IoT industry chain The Inte...
[[351012]] On November 6, a satellite named "...
[Ruijie Networks Press Release] On April 10, Ruij...