w3resource

Python Metaprogramming - Exercises, Practice, Solutions


Python Metaprogramming [12 exercises with solution]


[An editor is available at the bottom of the page to write and execute the scripts.   Go to the editor]

Python metaprogramming refers to the technique of writing code that manipulates or generates other pieces of code dynamically during runtime. Metaprogramming allows developers to create more flexible, reusable, and efficient code by automating common programming tasks. This includes generating boilerplate code, adding functionality to classes at runtime, or altering the behavior of functions.

Using metaclasses

Metaclasses allow us to customize the creation of classes in Python. By defining a metaclass for a class, you can control how the class is instantiated, its attributes, and its behavior. Metaclasses are often used for implementing frameworks, ORMs (Object-Relational Mappers), and other advanced programming patterns.

1. Write a Python program to create a metaclass "UpperAttrMeta" that converts all attribute names of a class to uppercase.
Click me to see the sample solution

2. Write a Python program to create a metaclass "ValidateAttrMeta" that ensures all attributes of a class are integers. Raise a TypeError if any attribute is not an integer.
Click me to see the sample solution

3. Write a Python program to create a metaclass SingletonMeta that ensures a class only has one instance (singleton pattern).
Click me to see the sample solution

4. Write a Python metaclass "AttrLoggingMeta" that logs every time an attribute is accessed or modified.
Click me to see the sample solution

Dynamic Class Creation

Python's dynamic nature allows you to modify classes and objects at runtime. You can add, remove, or modify attributes and methods of classes and objects dynamically using built-in functions such as 'setattr', 'getattr', and 'delattr'.

1. Write a Python function "create_class" that takes a class name and a dictionary of attributes and methods, and returns a dynamically created class with those attributes and methods.
Click me to see the sample solution

2. Write Python a function "add_method" that takes a class, a method name, and a method, and adds that method to the class.
Click me to see the sample solution

3. 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.
Click me to see the sample solution

4. Write a Python function "create_inherited_class" that takes a base class, a class name, and a dictionary of additional attributes and methods, and returns a dynamically created subclass.
Click me to see the sample solution

Code Generation

Python's "exec" and "eval" functions allow you to execute dynamically generated code at runtime. This feature can be used for generating code templates, parsing domain-specific languages (DSLs), or implementing code generation tools.

1. Write a Python function "generate_code" that takes a template string with placeholders and a dictionary of values, and returns the generated code with placeholders replaced by corresponding values.
Click me to see the sample solution

2. Write a Python function "generate_function" that takes a function name and a body as strings, and returns a dynamically generated function.
Click me to see the sample solution

3. Write a Python function "generate_inherited_class" that takes a class name, a base class, and a dictionary of attributes and methods, and returns a dynamically generated subclass.
Click me to see the sample solution

4. Write a Python function "generate_complex_function" that takes a function name, a list of parameter names, and a body as strings, and returns a dynamically generated function.
Click me to see the sample solution

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.