Course
Wrapper Classes
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Wrapper Classes
A function in Python is a first-order object. A function can have another function as its argument and wrap another function definition inside it. This helps in modifying a function without actually changing it. Such functions are called decorators.
This feature is also available for wrapping a class. This technique is used to manage the class after it is instantiated by wrapping its logic inside a decorator.
Example
def decorator_function(Wrapped): class Wrapper: def __init__(self,x): self.wrap = Wrapped(x) def print_name(self): return self.wrap.name return Wrapper @decorator_functionclass Wrapped: def __init__(self,x): self.name = x obj = Wrapped('TutorialsPoint')print(obj.print_name())
Here,
Wrapped
is the name of the class to be wrapped. It is passed as argument to a function. Inside the function, we have a Wrapper class, modify its behavior with the attributes of the passed class, and return the modified class. The returned class is instantiated and its method can now be called.When you execute this code, it will produce the following output
TutorialsPoint
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.