Category: 8. Metaprogramming & Exceptions
-
Python dataclass
Summary: in this tutorial, you’ll learn about the Python dataclass decorator and how to use it effectively. Introduction to the Python dataclass Python introduced the dataclass in version 3.7 (PEP 557). The dataclass allows you to define classes with less code and more functionality out of the box. The following defines a regular Person class with two instance attributes name and age:…
-
Python Metaclass Example
Summary: in this tutorial, you’ll learn about a Python metaclass example that creates classes with many features. Introduction to the Python metaclass example The following defines a Person class with two attributes name and age: Typically, when defining a new class, you need to: As you can see, it requires a lot of code. Imagine you want to define a…
-
Python Metaclass
Summary: in this tutorial, you’ll learn about the Python metaclass and understand how Python uses the metaclasses to create other classes. Introduction to the Python Metaclass A metaclass is a class that creates other classes. By default, Python uses the type metaclass to create other classes. For example, the following defines a Person class: When Python executes the code, it uses…
-
Python type Class
Summary: in this tutorial, you’ll learn about the Python type class and understand how Python uses the type class to create other classes. Introduction to the Python type class In Python, a class is an object of the class type. For example, the following defines the Person class with two methods __init__ and greeting: The Person class is an object of the class type as shown in the…
-
Python __new__
Summary: in this tutorial, you’ll learn about the Python __new__ method and understand how Python uses it to create a new object. Introduction to the Python __new__ method When you create an instance of a class, Python first calls the __new__() method to create the object and then calls the __init__() method to initialize the object’s attributes. The __new__() is a static method of…