Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More by Marcus Richards
Author:Marcus Richards
Language: eng
Format: epub
Tags: python for advanced programmers, python data analysis, python data structure, python object oriented, python programming language, python artificial intelligence ai, python professional
Publisher: Marcus Richards
Published: 2024-03-19T00:00:00+00:00
The metaclass
A metaclass is, for a class, what a class is to an example; that is, a metaclass is utilized to make classes, similarly as classes are utilized to make occasions. What's more, similarly as we can ask whether an example has a place with a class by utilizing isinstance(), we can ask whether a class object, (for example, dict, int, or SortedList) acquires another class utilizing issubclass().
The simplest use of metaclasses is to create custom classes that fit into Pythonâs standard ABC hierarchy. For example, to make SortedList a collections.
So... instead of inheriting the ABC (as we showed earlier), we can simplyregister the SortedList as a collections.Sequence:
class SortedList:
...
collections.Sequence.register(SortedList)
After the class is characterized typically, we register it with the collections.Sequence ABC. Enlisting a class like this makes it a virtual subclass. A virtual sub-class reports that it is a subclass of the class or classes it is enlisted with (e.g., utilizing isinstance() or issubclass()), however doesn't acquire any information or techniques from any of the classes it is enrolled with.
Enlisting a class like this gives a guarantee that the class gives the API of the classes it is enrolled with, yet doesn't give any ensure that it will respect its guarantee. One utilization of metaclasses is to give both a guarantee and an assurance about a class' API. Another utilization is to change a class somehow or another (like a class decorator does). Furthermore, obviously, metaclasses can be utilized for the two purposes simultaneously.
Assume we need to make a gathering of classes that all give burden() and spare() techniques. We can do that by creating a class that when utilized as a metaclass, watches that these strategies are available:
class LoadableSaveable(type):
def __init__(cls, classname, bases, dictionary): super().__init__(classname, bases, dictionary) assert hasattr(cls, "load") and
isinstance(getattr(cls, "load"), collections.Callable), ("class '" +
classname + "' must provide a load() method")
assert hasattr(cls, "save") and
isinstance(getattr(cls, "save"), collections.Callable), ("class '" +
classname + "' must provide a save() method")
Classes that are to fill in as metaclasses must acquire from a definitive metaclass base class, type, or one of its subclasses.
Notice that this class is called when classes that utilization it are started up, without a doubt not all the time, so the runtime cost is incredibly low. Notice additionally that we should play out the checks after the class has been made (utilizing the super() call), since at exactly that point will the class' properties be accessible in the classitself. (The qualities are in the word reference, yet we like to chip away at the genuine instated class when doing checks.)
We could have watched that the heap and spare properties are callable utilizing hasattr() to watch that they have the __call__ quality, yet we favor tocheck whether they are cases of collections.Callable. The collec-tions.Callable unique base class gives the guarantee (however no assurance) thatinstances of its subclasses (or virtual subclasses) are callable.
When the class has been made (utilizing type.__new__() or a reimplementation of __new__()), the metaclass is introduced by calling its __init__() technique. The contentions given to __init__() are cls, the class that is simply been made; classname, the class' name (likewise accessible from cls.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12399)
Hello! Python by Anthony Briggs(9751)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9637)
The Mikado Method by Ola Ellnestam Daniel Brolund(9632)
Dependency Injection in .NET by Mark Seemann(9163)
Hit Refresh by Satya Nadella(8672)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8148)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(8048)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7653)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7652)
Grails in Action by Glen Smith Peter Ledbrook(7565)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7390)
The Complete Stick Figure Physics Tutorials by Allen Sarah(6959)
The Kubernetes Operator Framework Book by Michael Dame(6807)
Exploring Deepfakes by Bryan Lyon and Matt Tora(6564)
Practical Computer Architecture with Python and ARM by Alan Clements(6521)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(6502)
Robo-Advisor with Python by Aki Ranin(6488)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6433)
