Decorators in Pythons | easy way to learn about decorators
Python Decorators - Easy Explanation with Example What are Python Decorators? In simple terms, Python decorators allow you to modify or enhance the behavior of functions or methods without directly changing their code. They act like wrappers that provide extra functionality around your original function. Example: Say Hello Function Let's take a simple function that says hello: def say_hello(): print("Hello!") Now, imagine you want to add something extra, like printing "Start" before saying hello and "End" after, without modifying the original say_hello function. This is where decorators come in. Creating a Decorator Here's how you can create a simple decorator: def my_decorator(func): def wrapper(): print("Start") func() # Calls the original say_hello function ...
Comments
Post a Comment