Created
July 10, 2022 14:36
-
-
Save danbailo/d95044b6a2daea2da036fe9ff57bf514 to your computer and use it in GitHub Desktop.
Simple decorator with iterators usage.
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
def is_admin(func): | |
def wrapper(*args, **kwargs): | |
for item in args: | |
if item['is_admin']: | |
yield item | |
return wrapper | |
@is_admin | |
def foo(user): | |
print(user) | |
for item in foo( | |
{'name': 'Daniel', 'is_admin': True}, | |
{'name': 'Josué', 'is_admin': False}, | |
{'name': 'Lucas', 'is_admin': True} | |
): | |
print(item) | |
# Will print only admin users. | |
# {'name': 'Daniel', 'is_admin': True} | |
# {'name': 'Lucas', 'is_admin': True} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment