Decorators
def vowel_counter(func_to_decorate):
vowel_count = 0
def wrapper(doc):
nonlocal vowel_count
vowels = "aeiou"
for char in doc:
if char in vowels:
vowel_count += 1
print(f"Vowel count: {vowel_count}")
return func_to_decorate(doc)
return wrapper
@vowel_counter
def process_doc(doc):
print(f"Document: {doc}")
process_doc("What")
# Vowel count: 1
# Document: What
process_doc("a wonderful")
# Vowel count: 5
# Document: a wonderful
process_doc("world")
# Vowel count: 6
# Document: worldIt's Just Syntactic Sugar
With Decorator
Without Decorator
Last updated