Skip to content

Instantly share code, notes, and snippets.

@ErisianArchitect
Created August 19, 2015 01:03
Show Gist options
  • Save ErisianArchitect/ddd8c32c9c627938d9bc to your computer and use it in GitHub Desktop.
Save ErisianArchitect/ddd8c32c9c627938d9bc to your computer and use it in GitHub Desktop.
#Testing out decorators
class decor:
def __init__(self, value_):
self.v = value_
def __call__(self, f):
def wrapped_(*args):
if 'n' in globals():
tmp = globals()['n']
globals()['n'] = self.v
f(*args)
globals()['n'] = tmp
else:
globals()['n'] = self.v
f(*args)
del globals()['n']
return wrapped_
class C:
def __init__(self):
self.variable = "self.variable"
def decora(f):
def wrapper_(self, *args, **kwargs):
print(self.variable)
f(self, *args, **kwargs)
return wrapper_
@decora
def d_test(self):
print("Done")
@decor("Tryed")
def test():
print(n)
n = "Goodbye"
try:
test()
except:
print("Error")
print(n)
try:
myC = C()
myC.d_test()
except:
print("Error")
input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment