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

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



Copyright Disclaimer:
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.