Summary

We looked at three of the special methods for class definition. The __call__() method is used when creating a callable. The callable is used to create functions that are stateful. Our primary example is a function that memoizes previous results.

The __enter__() and __exit__() methods are used to create a context manager. The context is used to handle processing that is localized to the body of a with statement. Most of our examples include input-output processing. Python also uses localized contexts for the decimal state. Other examples include making patches for unit testing purposes or acquiring and releasing locks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset