Creating dynamic attributes and methods
In this section, let’s explore how to create dynamic methods within classes. A dynamic method is a method created for a class during runtime, unlike the regular class methods that we create while coding within the class definition itself.
Dynamic methods are created to avoid modifying the structure or the original class definition once it is defined. Instead of modifying the class definition, we can define and call a runtime template method that will in turn create a dynamic method for the class.
Let’s start by creating a simple class definition for managing the coupons of ABC Megamart named SimpleCoupon
:
class SimpleCoupon:
'''attributes''''''
''''''methods''''''
pass
We did not define any attributes or methods for this class,...