Decorator Function in Python
Level 4128 keystrokes
This creates a decorator that converts output to uppercase, printing 'HELLO'. Decorators modify function behavior.
Python code
# A decorator is a function that takes a function and returns a new function1def uppercase(func): # wrapper is the new function that will replace the original2 def wrapper(): # Call the original function and convert result to uppercase3 return func().upper() # Return the wrapper function4 return wrapper5 # @uppercase is syntactic sugar for: greet = uppercase(greet)6@uppercase7def greet():8 return "hello"9 # Calling greet() actually calls wrapper(), which calls original greet()10print(greet())Output
HELLO
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
(× 7)× 7:× 3"× 2.× 1@× 1