w3resource

Python Dynamic Class Creation: Flexible Method Inclusion

Python Metaprogramming: Exercise-7 with Solution

Creating Classes with Methods from a Template:

Write a Python function “create_class_with_methods” that takes a class name, a dictionary of attributes, and a dictionary of methods, and returns a dynamically created class with those attributes and methods.

Sample Solution:

Python Code :

# Function to create a class dynamically with specified attributes and methods
def create_class_with_methods(name, attrs, methods):
    # Combine attributes and methods into a single dictionary
    attrs.update(methods)
    # Create a new class with the given name, inheriting from 'object', and using the combined dictionary
    return type(name, (object,), attrs)

# Define attributes for the dynamic class
attributes = {
    # Add an attribute 'species' with value 'Human'
    'species': 'Human',
    # Add an attribute 'age' with value 25
    'age': 25
}

# Define methods for the dynamic class
methods = {
    # Add a method 'greet' that returns a greeting string using the class attributes
    'greet': lambda self: f"Hello, I am a {self.species} and I am {self.age} years old."
}

# Create a class dynamically using the defined attributes and methods
DynamicClass = create_class_with_methods('DynamicClass', attributes, methods)

# Test the dynamic class
# Create an instance of the dynamically created class
instance = DynamicClass()
# Call the 'greet' method of the instance
print(instance.greet())  # Output: "Hello, I am a Human and I am 25 years old."

Output:

Hello, I am a Human and I am 25 years old.

Explanation:

  • Function Definition:
    • "create_class_with_methods" takes 'name' (the name of the class), 'attrs' (a dictionary of attributes), and 'methods' (a dictionary of methods).
  • Combine Attributes and Methods:
    • 'attrs.update(methods)' combines the attributes and methods into a single dictionary.
  • Create Class:
    • The 'type' function is used to create a new class with the given 'name', inheriting from 'object', and using the combined dictionary of attributes and methods.
  • Attributes Definition:
    • attributes is a dictionary containing:
      • species: An attribute with the value 'Human'.
      • age: An attribute with the value 25.
  • Methods Definition:
    • methods is a dictionary containing:
      • greet: A method that returns a greeting string using the 'species' and 'age' attributes.
  • Create Dynamic Class:
    • DynamicClass is created dynamically by calling "create_class_with_methods" with the class name, attributes, and methods.
  • Testing:
    • An instance of 'DynamicClass' is created.
    • The "greet" method of the instance is called, returning "Hello, I am a Human and I am 25 years old."

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Python Dynamic Method Addition: Expand Class Behavior.
Next: Python Dynamic Class Inheritance: Expand with Subclass.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/metaprogramming/python-metaprogramming-exercise-7.php