ANI

Checking Metaccolasse of Python: Opening the creative power of classes

Checking Metaccolasse of Python: Opening the creative power of classesImage by seated

Obvious Introduction

In Python, there is a sense called an OOP) program. This system paradigm rotates data and items. Works by betting a combination of integration (attributes) and behaviors (ways) within classes, and creates substance conditions in those classes.

For many data scientific, Python is the first planning program that they are learning, and becomes the basis for many data activities. However, sometimes, sometimes it is just considered to focus on the writing code of the process to solve the problems immediately. Data scientists, OOP allows a complex task to be torn down in manageable buildings. By editing the code in classes, data scientists can reduce and relax and improve maintenance.

Obvious Python Classes

Simple classes create in Python. The basic example is shown below.

class Animal:
    def __init__(self, name, sound):
        self.name = name
        self.sound = sound

    def speak(self):
        return f"{self.name} makes a sound {self.sound}."

We use the class syntax to describe the class name (“animal”) and capture the animal's concept with properties that we can enter as parameters, as well as the way I used it.

In the above class, we can build conditions for the item.

dog = Animal("Dog", "Bark")
print(dog.speak())

Release is displayed below.

This page Animal The class receives these two conflicts previously, and the item can use speak the way after we do it. The power of the class: Describe once and re-use it in many cases.

Obvious What is metaclasses?

Metaccolasse take things continuously by regulating how classes are created. You can think of metaclasses as text texts for classes, just as classes have colored documents. Just as an example of the class, the class is an example of metaclass. Metaccasses guide how classes behave.

When we built the class, Python made a class body and passed the metaclass result. Automatically, this is hosted by built-in type category. By separating typeWe can build our metaclasse and overwriting methods to customize creating classes.

Why can we want metaclass? There are several reasons related to the work of data scientists, including:

  • Providing standard framework for any of the following classes created
  • To ensure that all classes described the necessary methods
  • To avoid a repetitive set code

Let's look at how the MetAclass code code works, then we make down.

Obvious Metaclasses ython

When you make a metaclass, it looks like it creates a normal class, but you have inhabited from typeas shown below.

class ExampleMetaclass(type):
    def __new__(mcs, name, bases, namespace):
        def greet(self):
            return f"Hello from {self.__class__.__name__}!"
        namespace['greet'] = greet
        return super().__new__(mcs, name, bases, namespace)

Next, create a class like the previous example, but add a metaclass as additional parameter.

class Animal(metaclass=ExampleMetaclass):
    def __init__(self, name, sound):
        self.name = name
        self.sound = sound

    def speak(self):
        return f"{self.name} makes a sound {self.sound}."

The same in the same Animal Something as before, but now use the method provided by metaclass.

dog = Animal("Dog", "Bark")
print(dog.greet())

Release will be like:

As you can see, Animal Category at first we lack a greet Path, but the metaclass adds you to class during creation. Let's break a little bit.

First, creating a metaclass dying as type The paragraph provided by Python built in the internal. type Is the default metaclass for all Python classes and allows us to control how new classes are created.

Next, the __new__ The method in metaclass is destroyed when a new category is created. The parameters receive it:

  • mcs: Metaclass itself
  • name: The class name is defined (for example, “Animal”)
  • bases: The state of basic categories (classes where the new class finds)
  • namespace: A dictionary containing all the qualities and methods described within the class body

With four parameters, we can control how the metaclass formed the stone section.

Then, we explain the greet Work and Jogenge in the name of the phase name. By doing this, greet becomes part of the class, and all classes are created with ExampleMetaclass It will include you.

Finally, the metaclass creates a new class by calling superclass's __new__ the way.

That is the core of metaclasses ython, and you can add any of the methods or rules you need to measure and control class creation.

Obvious Rolling up

Python is widely used with data scientist. One important topic in Python is a program based on, especially metaclasses. By using metaclasses, we can control and modify the creative process, helps maintain the codebase consensus and improve performance functionality.

I hope this has helped!

Cornellius Yudha Wijaya It is a scientific science manager and the database author. While working full-time in Allianz Indonesia, she likes to share the python and data advice with social media and media writing. Cornellius writes to a variety of AI and a study machine.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button