Created
August 24, 2022 18:45
-
-
Save dword4/c44fcb3bb07a4e447808d0535925a2d3 to your computer and use it in GitHub Desktop.
decorator problems
This file contains 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
#!/usr/bin/env python3 | |
class command(object): | |
def ratelimit(*args, **kwargs): | |
limit = args[0] | |
def ratelimit_wrapper(func): | |
print(f"rate: {limit}") | |
print("begin: ratelimit_wrapper") | |
# some stuff | |
print("end: ratelimit_wrapper") | |
return func | |
return ratelimit_wrapper | |
class animals(object): | |
@command.ratelimit(10) | |
def cat(self): | |
print("meow") | |
@command.ratelimit(5) | |
def dog(self): | |
print("woof") | |
zoo = animals() | |
zoo.cat() | |
zoo.dog() | |
zoo.cat() |
This file contains 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
./dec.py | |
rate: 10 | |
begin: ratelimit_wrapper | |
end: ratelimit_wrapper | |
rate: 5 | |
begin: ratelimit_wrapper | |
end: ratelimit_wrapper | |
meow | |
woof | |
meow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment