-
-
Save staticor/9694dcc7a596b739af97 to your computer and use it in GitHub Desktop.
class decorator
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
class Tx(object): | |
def __init__(self): | |
print("Tx:init") | |
self.something() | |
def something(self): | |
print("Tx:something") | |
def otherthing(self): | |
print("Tx:otherthing") | |
def __call__(self, func): | |
def wrapper(s, url_list): | |
url_list.append(4) | |
self.otherthing() | |
return func(s, url_list) | |
return wrapper | |
class v2(object): | |
@Tx() | |
def run(self, url_list): | |
print("v2:run") | |
print(url_list) | |
x = v2() | |
x.run([1, 2, 3]) | |
##### | |
# OUTPUT | |
# | |
# Tx:init | |
# Tx:something | |
# Tx:otherthing | |
# v2:run | |
# [1, 2, 3, 4] | |
# | |
##### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment