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?
Over the past few decades, mobile communication t...
The latest 6G speed created by Chinese scientists...
Although the saying "no trouble, no failure&...
Overview of MAC Address Flapping MAC address drif...
As high-speed cellular networks become mainstream...
EU member states recently voted on a plan to adop...
IPv6 is the abbreviation of Internet Protocol Ver...
We had so much trouble getting security back to n...
Ericsson and Swisscom have signed an expanded 5G ...
BuyVM was founded in 2010. It is a company that p...
The concept of network automation has been around...
The rollout of 5G networks has been alarmingly sl...
Tribe shared the news about Olink.Cloud in Novemb...
Tudcloud has launched this month's regular pr...