134. Dynamic Class Creation with type
1. Basic Dynamic Class Creation
# Define the class name, base classes, and class body as a dictionary
class_name = "Person"
base_classes = (object,) # The base class for inheritance
class_body = {
"greet": lambda self: f"Hello, my name is {self.name}.",
}
# Dynamically create the class using type()
Person = type(class_name, base_classes, class_body)
# Instantiate the dynamically created class
person_instance = Person()
person_instance.name = "Alice"
# Call the method defined in the class body
print(person_instance.greet()) # Output: Hello, my name is Alice.2. Dynamically Creating a Class with Initialization Method
3. Creating a Class with Multiple Inheritance
4. Dynamically Creating a Class with Class Variables
5. Dynamic Class Creation for Different Data Types
6. Dynamic Class Creation with Multiple Methods
7. Creating an Abstract Class Dynamically
8. Dynamic Class Creation with Descriptors
9. Dynamic Class Creation with Static Methods
10. Dynamically Creating a Singleton Class
Conclusion:
Last updated