Skip to content

Instantly share code, notes, and snippets.

@Sllayan
Last active January 1, 2025 13:03
Show Gist options
  • Select an option

  • Save Sllayan/45d8dc7d8b47a2a5c72447907e0b0c07 to your computer and use it in GitHub Desktop.

Select an option

Save Sllayan/45d8dc7d8b47a2a5c72447907e0b0c07 to your computer and use it in GitHub Desktop.
Simple example of decorator in Python

Python decorator

This is an example of simple decorator in Python

# Decorator function
def my_decorator(func):
def wrapper():
print('Anything that needs to be done before the function is called.')
func()
print('Anything that needs to be done after the function is called.')
return wrapper
# First usecase
def say_hello():
print('hello')
say_it = my_decorator(say_hello)
say_it()
# Second usecase
@my_decorator
def say_hello():
print('hello')
say_hello()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment