Skip to content

Instantly share code, notes, and snippets.

@agronholm
Created November 10, 2019 13:05
Show Gist options
  • Save agronholm/8513b9ec9ce71384371b281ea984b5ef to your computer and use it in GitHub Desktop.
Save agronholm/8513b9ec9ce71384371b281ea984b5ef to your computer and use it in GitHub Desktop.
How do I properly wrap a class method?
from functools import wraps
class A:
@classmethod
def method(cls):
print('cls = %s' % cls.__name__)
class B(A):
pass
def wrapper(func):
@wraps(func)
def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner
A.method = wrapper(A.method)
B.method()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment