ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://wellsr.com/python/working-with-metaclasses-in-python/ |
| Last Crawled | 2026-04-11 14:52:05 (5 days ago) |
| First Indexed | 2023-08-18 01:03:02 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Working with Metaclasses in Python - Harnessing Advanced Object Control - wellsr.com |
| Meta Description | This article explains how to work with Python metaclasses. We'll explain what metaclasses are, how they differ from normal classes, and when you should use them. |
| Meta Canonical | null |
| Boilerpipe Text | Introduction
In this article, we will explore the concept of metaclasses, an advanced feature that grants you unparalleled control over class creation and behavior. We will dive into the intricacies of metaclasses, understanding what they are, how they differ from regular Python classes, and the situations where they prove the most valuable.
Meta classes are classes for classes. Regular classes create objects, but metaclasses create classes themselves. They act as blueprints for classes and control class-level operations during class creation.
A Basic Example
Let’s start with a simple example to grasp the workings of metaclasses:
class
Meta
(
type
):
def
__new__
(
cls
,
name
,
bases
,
dct
):
uppercase_attributes
=
{}
for
key
,
value
in
dct
.
items
():
if
not
key
.
startswith
(
"__"
):
uppercase_attributes
[
key
.
upper
()]
=
value
return
super
()
.
__new__
(
cls
,
name
,
bases
,
uppercase_attributes
)
class
MyClass
(
metaclass
=
Meta
):
x
=
10
y
=
20
print
(
MyClass
.
X
)
# Output: 10
print
(
MyClass
.
Y
)
# Output: 20
Output:
10
20
Here is the explanation of the above code:
We define a custom metaclass named
Meta
.
The
Meta
class overrides the
__new__()
method, which is responsible for creating a new class.
The
__new__
method receives four arguments:
cls
- the metaclass itself
name
- the name of the class being created
bases
- the base classes
dct
- the class attributes
We iterate through the
dct
dictionary and convert the attribute names to uppercase.
Finally, the
super().__new__()
method is called to create the new class with the modified attributes.
A Real-World Example
Now, let’s explore a more practical example of using metaclasses:
class
SingletonMeta
(
type
):
_instances
=
{}
def
__call__
(
cls
,
*
args
,
**
kwargs
):
if
cls
not
in
cls
.
_instances
:
cls
.
_instances
[
cls
]
=
super
()
.
__call__
(
*
args
,
**
kwargs
)
return
cls
.
_instances
[
cls
]
class
SingletonClass
(
metaclass
=
SingletonMeta
):
def
__init__
(
self
,
value
):
self
.
value
=
value
obj1
=
SingletonClass
(
1
)
obj2
=
SingletonClass
(
2
)
print
(
"Object1 Value = "
,
obj1
.
value
)
# Output: 1
print
(
"Object2 Value = "
,
obj2
.
value
)
# Output: 1 (both objects share the same instance)
Output:
Object1
Value
=
1
Object2
Value
=
1
The explanation of the above code is as follows:
We create a
SingletonMeta
metaclass that enforces the Singleton design pattern.
The
SingletonMeta
class overrides the
__call__
method, which is called when an instance of
SingletonClass
is created.
We use a dictionary,
_instances
, to store instances of each class created with the
SingletonMeta
metaclass.
When
__call__
method is invoked, it checks if an instance of the class already exists in
_instances
dictionary. If not, it creates a new instance using the
super().__call__
method and stores it in
_instances
.
Subsequent calls to create instances of
SingletonClass
return the same instance, ensuring there is only one instance of the class.
Differences from Normal Python Classes
To really understand the difference between a metaclass and a regular class, let’s take a step back. Imagine you’re baking. When you bake cookies, you use a cookie cutter to give shape to the cookies. In the world of Python, regular classes are like cookies, and metaclasses are like cookie cutters. While regular classes define how objects (or instances) behave, metaclasses define how classes themselves behave.
Let’s dive into the key differences:
Class Creation Mechanism
Normal Python Classes
Think of these as blueprints for creating objects. When you define a regular class in Python, it’s akin to sketching out a design for a building. By default, Python uses a built-in metaclass named
type
to give form to this blueprint.
class
Building
:
floors
=
3
MetaClasses
Metaclasses are like blueprints for the blueprints. They dictate how classes themselves are constructed. It’s like defining the rules and tools to be used while sketching out building designs.
class
MetaBuilder
(
type
):
pass
class
Building
(
metaclass
=
MetaBuilder
):
floors
=
3
Attribute Manipulation During Class Creation
To make this clearer, let’s consider a simple analogy.
Normal Python Classes
Imagine you’re crafting a sculpture. With a regular class, you’re directly molding the shape, adding details and creating features. However, once the sculpture is made, changing its features can be a challenge.
class
Sculpture
:
material
=
"clay"
height
=
"5ft"
MetaClasses
With a metaclass, it’s as if you’re defining the tools and techniques to be used for crafting sculptures. This gives you the ability to influence the outcome even before the sculpture is made.
For instance, a metaclass can ensure that all sculptures have a base:
class
SculptureTools
(
type
):
def
__new__
(
cls
,
name
,
bases
,
attrs
):
attrs
[
'base'
]
=
"wooden"
return
super
()
.
__new__
(
cls
,
name
,
bases
,
attrs
)
class
Sculpture
(
metaclass
=
SculptureTools
):
material
=
"clay"
height
=
"5ft"
Instance Creation vs. Class Creation
Normal Python Classes
Regular classes are used to create instances of objects. When you instantiate a regular class, it creates an instance of that class, i.e., an object.
class
Dog
:
def
__init__
(
self
,
name
):
self
.
name
=
name
#This is creating an instance of the Dog class
buddy
=
Dog
(
"Buddy"
)
print
(
buddy
.
name
)
Output:
Buddy
In the above script, we define a simple
Dog
class that has a
name
attribute. When we create a new
Dog
object (or instance) named
buddy
, we’re using the class as a blueprint to create this specific dog with the name “Buddy”.
MetaClasses
Meta classes are used to create classes, not instances. They define how classes are created and behave at the class level. Instances of classes created using a specific metaclass will have the characteristics and behavior defined by that metaclass.
class
Meta
(
type
):
def
__init__
(
cls
,
name
,
bases
,
dct
):
cls
.
class_name
=
name
class
Cat
(
metaclass
=
Meta
):
pass
#This does not create an instance, but the class itself has an attribute due to the metaclass
print
(
Cat
.
class_name
)
Output:
Cat
In this example, the
Meta
metaclass adds a
class_name
attribute to any class it helps create. The
Cat
class doesn’t have an instance created, but it still has the
class_name
attribute because of the metaclass.
Use Cases and Functionality
Normal Python Classes
Regular classes are the foundation of object-oriented programming in Python. They are used to create objects, define their properties (attributes), and behaviors (methods).
class
Car
:
def
__init__
(
self
,
make
,
model
):
self
.
make
=
make
self
.
model
=
model
def
drive
(
self
):
return
f
"{self.make} {self.model} is driving!"
my_car
=
Car
(
"Toyota"
,
"Corolla"
)
print
(
my_car
.
drive
())
Output:
Toyota
Corolla
is
driving
!
In the above example, we have a
Car
class that represents a car. Each car has a make and a model. The drive method returns a string indicating that the car is driving.
MetaClasses
Meta classes are more advanced and used for specialized purposes. They can be used to implement design patterns, enforce coding standards, automatically generate classes from external sources (e.g., database schema), and add functionality to multiple classes at once. Here’s an example
class
SingletonMeta
(
type
):
_instances
=
{}
def
__call__
(
cls
,
*
args
,
**
kwargs
):
if
cls
not
in
cls
.
_instances
:
cls
.
_instances
[
cls
]
=
super
()
.
__call__
(
*
args
,
**
kwargs
)
return
cls
.
_instances
[
cls
]
class
Singleton
(
metaclass
=
SingletonMeta
):
pass
singleton1
=
Singleton
()
singleton2
=
Singleton
()
print
(
singleton1
is
singleton2
)
Output:
True
In the above code, the
SingletonMeta
metaclass ensures that there’s only ever one instance of the Singleton class. Even when we try to create two separate instances (
singleton1
and
singleton2
), they both point to the same single instance. This is an example of using metaclasses to enforce a standard.
Metaclasses are a powerful tool, but they should be used judiciously. Some common use cases for metaclasses include:
API Design
Ensuring that classes adhere to a consistent interface:
class
InterfaceMeta
(
type
):
def
__init__
(
cls
,
name
,
bases
,
attrs
):
if
not
set
(
attrs
)
.
issuperset
({
"method1"
,
"method2"
}):
raise
TypeError
(
"Derived class must define method1 and method2!"
)
class
MyClass
(
metaclass
=
InterfaceMeta
):
def
method1
(
self
):
pass
def
method2
(
self
):
pass
In the example above, the
InterfaceMeta
metaclass ensures that any class that uses it as a metaclass implements both
method1
and
method2
. If a class tries to use this metaclass without implementing both methods, a
TypeError
is raised. This is useful when [designing an API](/python/python-interact-with-api-using-http-requests/.
ORM (Object-Relational Mapping) Systems
Automatically Generating Classes from Database Schema
class
ORMMeta
(
type
):
# Mock database schema
SCHEMA
=
{
"User"
:
[
"name"
,
"email"
],
"Product"
:
[
"title"
,
"price"
]
}
def
__new__
(
cls
,
name
,
bases
,
attrs
):
if
name
in
cls
.
SCHEMA
:
for
field
in
cls
.
SCHEMA
[
name
]:
attrs
[
field
]
=
None
return
super
()
.
__new__
(
cls
,
name
,
bases
,
attrs
)
class
User
(
metaclass
=
ORMMeta
):
pass
class
Product
(
metaclass
=
ORMMeta
):
pass
print
(
hasattr
(
User
,
"name"
))
print
(
hasattr
(
Product
,
"price"
))
Output:
True
True
The
ORMMeta
metaclass simulates an ORM by automatically adding attributes to classes based on a mock database schema. Here, the
User
class gets name and email attributes, and the
Product
class gets title and price attributes due to the metaclass.
Validation and Error Checking
Adding automatic validation to class attributes
class
ValidateMeta
(
type
):
def
__init__
(
cls
,
name
,
bases
,
attrs
):
if
"age"
in
attrs
and
not
isinstance
(
attrs
[
"age"
],
int
):
raise
ValueError
(
"The age attribute must be an integer!"
)
class
Person
(
metaclass
=
ValidateMeta
):
age
=
25
class
InvalidPerson
(
metaclass
=
ValidateMeta
):
age
=
"twenty-five"
# Raises ValueError
Output:
ValueError
Traceback
(
most
recent
call
last
)
~
\
AppData
\
Local
\
Temp
\
ipykernel_14936
\
1974465106.
py
in
<
module
>
9
10
--->
11
class
InvalidPerson
(
metaclass
=
ValidateMeta
):
12
age
=
"twenty-five"
# Raises ValueError
~
\
AppData
\
Local
\
Temp
\
ipykernel_14936
\
1974465106.
py
in
__init__
(
cls
,
name
,
bases
,
attrs
)
2
def
__init__
(
cls
,
name
,
bases
,
attrs
):
3
if
"age"
in
attrs
and
not
isinstance
(
attrs
[
"age"
],
int
):
---->
4
raise
ValueError
(
"The age attribute must be an integer!"
)
5
6
ValueError
:
The
age
attribute
must
be
an
integer
!
In the above code, the
ValidateMeta
metaclass checks if the age attribute of a class is an integer. If not, it raises a
ValueError
. In the example, the
Person
class is valid because its
age
attribute is an integer, but the
InvalidPerson
class raises an error because its age attribute is a string.
Conclusion
In summary, while metaclasses offer tremendous capabilities for advanced customization of classes, they should be used judiciously and sparingly. Only resort to metaclasses when standard object-oriented techniques fall short or when the unique capabilities of metaclasses are required for your specific use case. By applying metaclasses thoughtfully and selectively, you can leverage their power to build elegant, extensible, and maintainable Python codebases.
This article was written by Usman Malik, contributing writer for The Python Tutorials Blog. |
| Markdown | [wellsr.com](https://wellsr.com/)
- [VBA Tutorials](https://wellsr.com/vba/)
- [Python Tutorials](https://wellsr.com/python/)
- [VBA Cheat Sheets](https://wellsr.com/vba/vba-cheat-sheets/bundle/)
- [Python Cheat Sheets](https://wellsr.com/python/python-cheat-sheets/dev-kit/)
# Working with Metaclasses in Python - Harnessing Advanced Object Control
[The Python Tutorials Blog](https://wellsr.com/python/)
Aug 17, 2023
*Looking for [VBA Tutorials](https://wellsr.com/vba/)?*
## Introduction
In this article, we will explore the concept of metaclasses, an advanced feature that grants you unparalleled control over class creation and behavior. We will dive into the intricacies of metaclasses, understanding what they are, how they differ from regular Python classes, and the situations where they prove the most valuable.
## Creating Meta Classes in Python
Meta classes are classes for classes. Regular classes create objects, but metaclasses create classes themselves. They act as blueprints for classes and control class-level operations during class creation.
### A Basic Example
Let’s start with a simple example to grasp the workings of metaclasses:
```
class Meta(type):
def __new__(cls, name, bases, dct):
uppercase_attributes = {}
for key, value in dct.items():
if not key.startswith("__"):
uppercase_attributes[key.upper()] = value
return super().__new__(cls, name, bases, uppercase_attributes)
class MyClass(metaclass=Meta):
x = 10
y = 20
print(MyClass.X) # Output: 10
print(MyClass.Y) # Output: 20
```
**Output:**
```
10
20
```
Here is the explanation of the above code:
- We define a custom metaclass named `Meta`.
- The `Meta` class overrides the `__new__()` method, which is responsible for creating a new class.
- The `__new__` method receives four arguments:
- `cls` - the metaclass itself
- `name` - the name of the class being created
- `bases` - the base classes
- `dct` - the class attributes
- We iterate through the `dct` dictionary and convert the attribute names to uppercase.
- Finally, the `super().__new__()` method is called to create the new class with the modified attributes.
### A Real-World Example
Now, let’s explore a more practical example of using metaclasses:
```
class SingletonMeta(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
class SingletonClass(metaclass=SingletonMeta):
def __init__(self, value):
self.value = value
obj1 = SingletonClass(1)
obj2 = SingletonClass(2)
print("Object1 Value = ",obj1.value) # Output: 1
print("Object2 Value = ",obj2.value) # Output: 1 (both objects share the same instance)
```
**Output:**
```
Object1 Value = 1
Object2 Value = 1
```
The explanation of the above code is as follows:
- We create a `SingletonMeta` metaclass that enforces the Singleton design pattern.
- The `SingletonMeta` class overrides the` __call__` method, which is called when an instance of `SingletonClass` is created.
- We use a dictionary, `_instances`, to store instances of each class created with the `SingletonMeta` metaclass.
- When `__call__` method is invoked, it checks if an instance of the class already exists in `_instances` dictionary. If not, it creates a new instance using the `super().__call__` method and stores it in `_instances`.
- Subsequent calls to create instances of `SingletonClass` return the same instance, ensuring there is only one instance of the class.
## Differences from Normal Python Classes
To really understand the difference between a metaclass and a regular class, let’s take a step back. Imagine you’re baking. When you bake cookies, you use a cookie cutter to give shape to the cookies. In the world of Python, regular classes are like cookies, and metaclasses are like cookie cutters. While regular classes define how objects (or instances) behave, metaclasses define how classes themselves behave.
Let’s dive into the key differences:
### Class Creation Mechanism
#### Normal Python Classes
Think of these as blueprints for creating objects. When you define a regular class in Python, it’s akin to sketching out a design for a building. By default, Python uses a built-in metaclass named *type* to give form to this blueprint.
```
class Building:
floors = 3
```
#### MetaClasses
Metaclasses are like blueprints for the blueprints. They dictate how classes themselves are constructed. It’s like defining the rules and tools to be used while sketching out building designs.
```
class MetaBuilder(type):
pass
class Building(metaclass=MetaBuilder):
floors = 3
```
### Attribute Manipulation During Class Creation
To make this clearer, let’s consider a simple analogy.
#### Normal Python Classes
Imagine you’re crafting a sculpture. With a regular class, you’re directly molding the shape, adding details and creating features. However, once the sculpture is made, changing its features can be a challenge.
```
class Sculpture:
material = "clay"
height = "5ft"
```
#### MetaClasses
With a metaclass, it’s as if you’re defining the tools and techniques to be used for crafting sculptures. This gives you the ability to influence the outcome even before the sculpture is made. For instance, a metaclass can ensure that all sculptures have a base:
```
class SculptureTools(type):
def __new__(cls, name, bases, attrs):
attrs['base'] = "wooden"
return super().__new__(cls, name, bases, attrs)
class Sculpture(metaclass=SculptureTools):
material = "clay"
height = "5ft"
```
### Instance Creation vs. Class Creation
#### Normal Python Classes
Regular classes are used to create instances of objects. When you instantiate a regular class, it creates an instance of that class, i.e., an object.
```
class Dog:
def __init__(self, name):
self.name = name
#This is creating an instance of the Dog class
buddy = Dog("Buddy")
print(buddy.name)
```
**Output:**
```
Buddy
```
In the above script, we define a simple `Dog` class that has a `name` attribute. When we create a new `Dog` object (or instance) named `buddy`, we’re using the class as a blueprint to create this specific dog with the name “Buddy”.
#### MetaClasses
Meta classes are used to create classes, not instances. They define how classes are created and behave at the class level. Instances of classes created using a specific metaclass will have the characteristics and behavior defined by that metaclass.
```
class Meta(type):
def __init__(cls, name, bases, dct):
cls.class_name = name
class Cat(metaclass=Meta):
pass
#This does not create an instance, but the class itself has an attribute due to the metaclass
print(Cat.class_name)
```
**Output:**
```
Cat
```
In this example, the `Meta` metaclass adds a `class_name` attribute to any class it helps create. The `Cat` class doesn’t have an instance created, but it still has the `class_name` attribute because of the metaclass.
### Use Cases and Functionality
#### Normal Python Classes
Regular classes are the foundation of object-oriented programming in Python. They are used to create objects, define their properties (attributes), and behaviors (methods).
```
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def drive(self):
return f"{self.make} {self.model} is driving!"
my_car = Car("Toyota", "Corolla")
print(my_car.drive())
```
**Output:**
```
Toyota Corolla is driving!
```
In the above example, we have a `Car` class that represents a car. Each car has a make and a model. The drive method returns a string indicating that the car is driving.
#### MetaClasses
Meta classes are more advanced and used for specialized purposes. They can be used to implement design patterns, enforce coding standards, automatically generate classes from external sources (e.g., database schema), and add functionality to multiple classes at once. Here’s an example
```
class SingletonMeta(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
class Singleton(metaclass=SingletonMeta):
pass
singleton1 = Singleton()
singleton2 = Singleton()
print(singleton1 is singleton2)
```
**Output:**
```
True
```
In the above code, the `SingletonMeta` metaclass ensures that there’s only ever one instance of the Singleton class. Even when we try to create two separate instances (`singleton1` and `singleton2`), they both point to the same single instance. This is an example of using metaclasses to enforce a standard.
## When and Why to Use Metaclasses
Metaclasses are a powerful tool, but they should be used judiciously. Some common use cases for metaclasses include:
### API Design
#### Ensuring that classes adhere to a consistent interface:
```
class InterfaceMeta(type):
def __init__(cls, name, bases, attrs):
if not set(attrs).issuperset({"method1", "method2"}):
raise TypeError("Derived class must define method1 and method2!")
class MyClass(metaclass=InterfaceMeta):
def method1(self):
pass
def method2(self):
pass
```
In the example above, the `InterfaceMeta` metaclass ensures that any class that uses it as a metaclass implements both `method1` and `method2`. If a class tries to use this metaclass without implementing both methods, a `TypeError` is raised. This is useful when \[designing an API\](/python/python-interact-with-api-using-http-requests/.
### ORM (Object-Relational Mapping) Systems
#### Automatically Generating Classes from Database Schema
```
class ORMMeta(type):
# Mock database schema
SCHEMA = {
"User": ["name", "email"],
"Product": ["title", "price"]
}
def __new__(cls, name, bases, attrs):
if name in cls.SCHEMA:
for field in cls.SCHEMA[name]:
attrs[field] = None
return super().__new__(cls, name, bases, attrs)
class User(metaclass=ORMMeta):
pass
class Product(metaclass=ORMMeta):
pass
print(hasattr(User, "name"))
print(hasattr(Product, "price"))
```
**Output:**
```
True
True
```
The `ORMMeta` metaclass simulates an ORM by automatically adding attributes to classes based on a mock database schema. Here, the `User` class gets name and email attributes, and the `Product` class gets title and price attributes due to the metaclass.
### Validation and Error Checking
#### Adding automatic validation to class attributes
```
class ValidateMeta(type):
def __init__(cls, name, bases, attrs):
if "age" in attrs and not isinstance(attrs["age"], int):
raise ValueError("The age attribute must be an integer!")
class Person(metaclass=ValidateMeta):
age = 25
class InvalidPerson(metaclass=ValidateMeta):
age = "twenty-five" # Raises ValueError
```
**Output:**
```
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_14936\1974465106.py in <module>
9
10
---> 11 class InvalidPerson(metaclass=ValidateMeta):
12 age = "twenty-five" # Raises ValueError
~\AppData\Local\Temp\ipykernel_14936\1974465106.py in __init__(cls, name, bases, attrs)
2 def __init__(cls, name, bases, attrs):
3 if "age" in attrs and not isinstance(attrs["age"], int):
----> 4 raise ValueError("The age attribute must be an integer!")
5
6
ValueError: The age attribute must be an integer!
```
In the above code, the `ValidateMeta` metaclass checks if the age attribute of a class is an integer. If not, it raises a `ValueError`. In the example, the `Person` class is valid because its `age` attribute is an integer, but the `InvalidPerson` class raises an error because its age attribute is a string.
## Conclusion
In summary, while metaclasses offer tremendous capabilities for advanced customization of classes, they should be used judiciously and sparingly. Only resort to metaclasses when standard object-oriented techniques fall short or when the unique capabilities of metaclasses are required for your specific use case. By applying metaclasses thoughtfully and selectively, you can leverage their power to build elegant, extensible, and maintainable Python codebases.
[« Working with NoSQL Databases in Python - MongoDB Examples](https://wellsr.com/python/mongadb-working-with-nosql-databases-in-python/ "This tutorial explains how to work with NoSQL databases in Python. You will see examples of performing CRUD operations with MongoDB using the PyMongo Module.")
[Handling Files and Folders in Python with Pathlib »](https://wellsr.com/python/handling-files-and-folders-in-python-with-pathlib-module/ "In this article, we'll explain how to work with the Python Pathlib module to perform basic and advanced file and directory operations and why it's preferred over the os.path module.")
***
This article was written by Usman Malik, contributing writer for The Python Tutorials Blog.
***
#### About The Python Tutorials Blog

*[The Python Tutorials Blog](https://wellsr.com/python/) was created by Ryan Wells, a Nuclear Engineer and professional VBA Developer. After launching his [VBA Tutorials Blog](https://wellsr.com/vba/) in 2015, he designed some [VBA Cheat Sheets](https://wellsr.com/vba/vba-cheat-sheets/bundle/), which have helped thousands learn to write better macros. He expanded in 2018 with The Python Tutorials Blog to teach people Python in a similar systematic way. Today, wellsr.com reaches over 2 million programmers each year\!*
New to Python? Start here
Chapter 1: Python Basics
[Getting Started with Python](https://wellsr.com/python/basics/getting-started-with-python-introduction/) [Python Data Types](https://wellsr.com/python/basics/python-data-types/) [Python Data Structures](https://wellsr.com/python/basics/python-data-structures/) [Python Conditionals, Loops & Logical Operators](https://wellsr.com/python/basics/python-conditionals-loops-and-logical-operators/)
Chapter 2: Intermediate Python
[Python Functions](https://wellsr.com/python/basics/using-python-functions-defining-new-functions/) [Python Modules](https://wellsr.com/python/basics/python-modules-for-saving-and-distributing-code/) [Python Classes](https://wellsr.com/python/basics/python-class-objects-and-class-applications/) [Python File I/O](https://wellsr.com/python/basics/python-io-input-output-examples/) [Python Namespaces](https://wellsr.com/python/basics/python-namespaces-variable-locations-and-scopes/)
Support Ad-Free Coding
Love learning without ads? Support us by checking out our **Python Developer Kit**. Your help keeps our site ad-free\!
[Get Your Tools Now](https://wellsr.com/python/python-cheat-sheets/dev-kit/)
Popular Python Tutorials
[3 Ways to Calculate Python Execution Time](https://wellsr.com/python/3-ways-to-calculate-python-execution-time/) [Implementing an Artificial Neural Network from Scratch in Python](https://wellsr.com/python/artificial-neural-network-from-scratch-in-python/) [How to Create Custom Modules in Python (with examples)](https://wellsr.com/python/how-to-create-custom-modules-in-python/) [Upsampling and Downsampling Imbalanced Data in Python](https://wellsr.com/python/upsampling-and-downsampling-imbalanced-data-in-python/) [Object Detection from Webcams with YOLO using Python](https://wellsr.com/python/object-detection-from-webcams-with-yolo/) [How to Read PDF Files with Python using PyPDF2](https://wellsr.com/python/read-pdf-files-with-python-using-pypdf2/) [Drawing Multiple Plots with Matplotlib in Python](https://wellsr.com/python/drawing-multiple-plots-with-matplotlib-in-python/) [Pickling and Unpickling Objects with Python Pickle Module](https://wellsr.com/python/pickling-and-unpickling-objects-with-python-pickle-module)
Latest Python Tutorials
[Building a MySQL Agent in Python LangGraph Framework](https://wellsr.com/python/developing-a-mysql-agent-in-langgraph/) [Developing a Simple Login Page with Python Django Framework](https://wellsr.com/python/developing-a-simple-login-page-with-python-django-framework/) [Extracting Structured Data Using LLMs via Hugging Face API](https://wellsr.com/python/extracting-structured-data-using-llms-via-hugging-face-inference-api/) [Multiagent Workflow in LangGraph with DeepSeek R1 Model](https://wellsr.com/python/multiagent-workflow-in-langgraph-with-deepseek-r1-model/) [Retrieval Augmented Generation with LangGraph Agents](https://wellsr.com/python/retrieval-augmented-generation-with-langgraph-agents/) [How to Develop Multi-agent Chatbots with LangGraph Agents](https://wellsr.com/python/multi-agent-chatbot-with-langgraph-agents/)
Cohoist
A new project from the creator of this site
Cohoist – modern case management for human services nonprofits
Over 10 years ago, I started wellsr.com to help people learn to code for free.
That same passion for helping others through programming led us to create **[Cohoist](https://cohoist.com/?utm_source=wellsr&utm_medium=sidebar&utm_campaign=wellsr_sidebar&utm_id=wellsr)** – a modern case management platform that supports nonprofits in serving clients, tracking assistance, creating reports, and demonstrating their impact.
If you know a nonprofit that might find it useful, I'd really appreciate you mentioning Cohoist to them.
\- Ryan Wells
[Visit Cohoist.com](https://cohoist.com/?utm_source=wellsr&utm_medium=sidebar&utm_campaign=wellsr_sidebar&utm_id=wellsr)
## The Python Tutorials Blog with Ryan Wells
- Working with Metaclasses in Python - Harnessing Advanced Object Control
- [Privacy Policy](https://wellsr.com/python/privacy-policy/)
- [RSS](https://wellsr.com/python/feed.xml)
- [Twitter](https://twitter.com/ryanwellsr)
- [GitHub](https://github.com/ryanwellsr)
- [LinkedIn](https://linkedin.com/in/ryanwellsr)
Each Python Tutorial contains examples to help you learn Python programming quickly. Follow these Python tutorials to learn basic and advanced Python programming. |
| Readable Markdown | null |
| Shard | 129 (laksa) |
| Root Hash | 222858197617463129 |
| Unparsed URL | com,wellsr!/python/working-with-metaclasses-in-python/ s443 |