Created
May 16, 2022 08:09
-
-
Save mebaysan/032a1b5162ec902d2c098c6cfd54e9ef to your computer and use it in GitHub Desktop.
Python decorator example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person: | |
def __init__(self,name,age): | |
self.name = name | |
self.age = age | |
def check_age(func): | |
def wrapper(person,*args,**kwargs): | |
if person.age < 18: | |
print(f'You can not enter the party {person.name} because you are younger than 18!') | |
return False | |
else: | |
func(person,*args,**kwargs) | |
return wrapper | |
@check_age | |
def greeting(person): | |
print(f"Welcome to the party {person.name}!") | |
person1 = Person('Enes',22) | |
person2 = Person('Faris',17) | |
greeting(person1) | |
greeting(person2) | |
""" | |
>>> Welcome to the party Enes! | |
>>> You can not enter the party Faris because you are younger than 18! | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment