Skip to main content
make it more clear where the exclamation point is coming from
Source Link
maxywb
  • 2.6k
  • 2
  • 20
  • 26

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def wrapper(method): @wraps(method) def _impl(self, *method_args, **method_kwargs): returnmethod_output = method(self, *method_args, **method_kwargs) return method_output + "!" return _impl class Foo: @wrapper def bar(self, word): return word f = Foo() result = f.bar("kitty") print(result) 

Which will print:

kitty! 

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def wrapper(method): @wraps(method) def _impl(self, *method_args, **method_kwargs): return method(self, *method_args, **method_kwargs) + "!" return _impl class Foo: @wrapper def bar(self, word): return word f = Foo() result = f.bar("kitty") print(result) 

Which will print:

kitty! 

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def wrapper(method): @wraps(method) def _impl(self, *method_args, **method_kwargs): method_output = method(self, *method_args, **method_kwargs) return method_output + "!" return _impl class Foo: @wrapper def bar(self, word): return word f = Foo() result = f.bar("kitty") print(result) 

Which will print:

kitty! 
remove weird name, nad show how functool wrap can do something beyond being a passthrough
Source Link
maxywb
  • 2.6k
  • 2
  • 20
  • 26

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def catcherwrapper(method): @wraps(method) def _impl(self, *method_args, **method_kwargs):   return method(self, *method_args, **method_kwargs) + "!" return _impl class Foo: @catcher@wrapper def byebar(self, word): print("bye",return word) f = Foo() result = f.byebar("kitty") print(result) 

Which will print:

kitty! 

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def catcher(method): @wraps(method) def _impl(self, *method_args, **method_kwargs): method(self, *method_args, **method_kwargs) return _impl class Foo: @catcher def bye(self, word): print("bye", word) f = Foo() f.bye("kitty") 

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def wrapper(method): @wraps(method) def _impl(self, *method_args, **method_kwargs):   return method(self, *method_args, **method_kwargs) + "!" return _impl class Foo: @wrapper def bar(self, word): return word f = Foo() result = f.bar("kitty") print(result) 

Which will print:

kitty! 
remove unnecessary example method
Source Link
maxywb
  • 2.6k
  • 2
  • 20
  • 26

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def catcher(method): @wraps(method) def _impl(self, *method_args, **method_kwargs): method(self, *method_args, **method_kwargs) return _impl class Foo: @catcher def bye(self, word): print("bye", word)  @catcher def kill(self): raise ValueError("lol") f = Foo() f.bye("kitty") f.kill() 

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def catcher(method): @wraps(method) def _impl(self, *method_args, **method_kwargs): method(self, *method_args, **method_kwargs) return _impl class Foo: @catcher def bye(self, word): print("bye", word)  @catcher def kill(self): raise ValueError("lol") f = Foo() f.bye("kitty") f.kill() 

A more concise example might be as follows:

#/usr/bin/env python3 from functools import wraps def catcher(method): @wraps(method) def _impl(self, *method_args, **method_kwargs): method(self, *method_args, **method_kwargs) return _impl class Foo: @catcher def bye(self, word): print("bye", word) f = Foo() f.bye("kitty") 
Source Link
maxywb
  • 2.6k
  • 2
  • 20
  • 26
Loading