Would something like this do what you need?

class Test(object):
    def _decorator(foo):
        def magic( self ) :
            print "start magic"
            foo( self )
            print "end magic"
        return magic

    @_decorator
    def bar( self ) :
        print "normal call"

test = Test()

test.bar()

This avoids the call to self to access the decorator and leaves it hidden in the class namespace as a regular method.

>>> import stackoverflow
>>> test = stackoverflow.Test()
>>> test.bar()
start magic
normal call
end magic
>>> 

edited to answer question in comments:

How to use the hidden decorator in another class

class Test(object):
    def _decorator(foo):
        def magic( self ) :
            print "start magic"
            foo( self )
            print "end magic"
        return magic

    @_decorator
    def bar( self ) :
        print "normal call"

    _decorator = staticmethod( _decorator )

class TestB( Test ):
    @Test._decorator
    def bar( self ):
        print "override bar in"
        super( TestB, self ).bar()
        print "override bar out"

print "Normal:"
test = Test()
test.bar()
print

print "Inherited:"
b = TestB()
b.bar()
print

Output:

Normal:
start magic
normal call
end magic

Inherited:
start magic
override bar in
start magic
normal call
end magic
override bar out
end magic
Answer from Michael Speer on Stack Overflow
🌐
Tutorial Teacher
tutorialsteacher.com › python › classmethod-decorator
Python Class Method Decorator @classmethod
In Python, the @classmethod decorator is used to declare a method in the class as a class method that can be called using ClassName.MethodName().
🌐
Reddit
reddit.com › r/learnpython › confused about decorators, classmethod and staticmethod
r/learnpython on Reddit: Confused about decorators, classmethod and staticmethod
December 24, 2023 -

Hi r/learnpython, I'm currently working through djangoforbeginners and encountered the usage of classmethod() as a decorator for testing using setUpTestData().

I'm really trying to get comfortable reading the docs, but I feel like the explanations are using other terms I don't really understand to explain it, and feel like it's just looping into each other without a dumbed down explanation.

From what I could gather reading the docs, decorators make the code look neater in a similar(?) sense that list comprehension make it easier to read? I also don't quite understand what classmethod and staticmethod does that's different to just defining your own function. What do they mean by staticmethod not receiving an "implicit argument" vs. classmethod which does?

Please help an absolute clean slate beginner here! TIA.

Top answer
1 of 5
9
I recommend having a look at the Pythons class development YouTube lecture by Raymond Hettinger who is a Python core developer. The talk is old and is about Python 2 but can be easily adapted to Python 3. In essence most methods are instance methods and are designed to access instance data: class CustomClass(object): def __init__(x, y): self.x = x self.y = y def x_squared(self): return self.x ** 2 self essentially means "this instance" and so: this_instance = CustomClass(3, 4) When the method is called from the class, an instance needs to be supplied: CustomClass.x_squared(self=this_instance) The method can then work on the instance data. Normally self is provided positionally. When a method is called from an instance, the instance is implied: this_instance.x_squared() And therefore the instance data from this_instance will be used. The main use of a class method is an alternative constructor: class CustomClass(object): def __init__(x, y): self.x = x self.y = y @classmethod def from_inverse(cls, a, b): x = 1 / a y = 1 / b return CustomClass(x, y) This means a new CustomClass can be created using: this_instance = CustomClass(x=3, y=4) Or the alternative constructor: that_instance = CustomClass.from_inverse(a=1/3, b=1/3) Notice that the class method is called from the class and does not have an instance. The class method can be called from an instance although it is not common to do so: that_instance = this_instance.from_inverse(a=1/3, b=1/3) However all it does is determine the type of the class from the instance and invoke the class method and does not access instance data. Examples of this are in the datetime class (in the datetime module) which has from_isoformat, from_isocalendar, from_ordinal and from_timestamp which each act as alternative constructors for the datetime class from differing input formats. Notice most the alternative constructors have from in the method name. now is also an alternative constructor, constructing a datetime instance from the system clock. The final method, the static method is neither bound to a class or a method. It is far less commonly used. The static method is a regular function found in the namespace of a class and is only placed there for convenience. Notice that it neither has self (an instance) or cls (a class) as the first input argument and is therefore not bound to an instance or a class: class CustomClass(object): def __init__(x, y): self.x = x self.y = y @staticmethod def some_function(c, d): return c + d The video tutorial by Raymond Hettinger will give you a better understanding.
2 of 5
2
Thank you everyone who have put so much thought into their responses. I'm sure all your explanations have been broken down into the simplest way possible, I think I'll need to spend more time experimenting with all your examples to get my head around. Thanks!
Discussions

python - Class method decorator with self arguments? - Stack Overflow
How do I pass a class field to a decorator on a class method as an argument? What I want to do is something like: class Client(object): def __init__(self, url): self.url = url @ More on stackoverflow.com
🌐 stackoverflow.com
Decorators and class methods
Don't fall into the DRY trap. In this case, a little repetition is way better than the alternatives (e.g. __getattribute__ and metaclasses). More on reddit.com
🌐 r/learnpython
23
4
April 16, 2024
The best explanation of Python decorators I’ve ever seen. (An archived answer from StackOverflow.) · GitHub
From the same author: generators (yield): https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do/231855#231855 metaclasses: https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python/6581949#6581949 The guy wants to write a book, so if an editor comes around, you know what do to... More on reddit.com
🌐 r/Python
12
87
May 16, 2018
Can you explain what decorator is like I’m 5?

From a few years ago, an explanation people seemed to like.

More on reddit.com
🌐 r/Python
22
16
July 30, 2018
Top answer
1 of 15
386

Would something like this do what you need?

class Test(object):
    def _decorator(foo):
        def magic( self ) :
            print "start magic"
            foo( self )
            print "end magic"
        return magic

    @_decorator
    def bar( self ) :
        print "normal call"

test = Test()

test.bar()

This avoids the call to self to access the decorator and leaves it hidden in the class namespace as a regular method.

>>> import stackoverflow
>>> test = stackoverflow.Test()
>>> test.bar()
start magic
normal call
end magic
>>> 

edited to answer question in comments:

How to use the hidden decorator in another class

class Test(object):
    def _decorator(foo):
        def magic( self ) :
            print "start magic"
            foo( self )
            print "end magic"
        return magic

    @_decorator
    def bar( self ) :
        print "normal call"

    _decorator = staticmethod( _decorator )

class TestB( Test ):
    @Test._decorator
    def bar( self ):
        print "override bar in"
        super( TestB, self ).bar()
        print "override bar out"

print "Normal:"
test = Test()
test.bar()
print

print "Inherited:"
b = TestB()
b.bar()
print

Output:

Normal:
start magic
normal call
end magic

Inherited:
start magic
override bar in
start magic
normal call
end magic
override bar out
end magic
2 of 15
65

What you're wanting to do isn't possible. Take, for instance, whether or not the code below looks valid:

class Test(object):

    def _decorator(self, foo):
        foo()

    def bar(self):
        pass
    bar = self._decorator(bar)

It, of course, isn't valid since self isn't defined at that point. The same goes for Test as it won't be defined until the class itself is defined (which its in the process of). I'm showing you this code snippet because this is what your decorator snippet transforms into.

So, as you can see, accessing the instance in a decorator like that isn't really possible since decorators are applied during the definition of whatever function/method they are attached to and not during instantiation.

If you need class-level access, try this:

class Test(object):

    @classmethod
    def _decorator(cls, foo):
        foo()

    def bar(self):
        pass
Test.bar = Test._decorator(Test.bar)
🌐
Built In
builtin.com › software-engineering-perspectives › python-class-decorator
Python Class Decorators: A Guide | Built In
In both cases, decorating adds functionality to existing functions. When we decorate a function with a class, that function becomes an instance of the class. When we define methods in the decorating class, we can add functionality to the function.
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Classes › ClassDecorators.html
20.15. Class Decorators — Foundations of Python Programming
Recall that Python has a “decorator” syntax” that allows us to modify the behavior of functions. We can use this same syntax to modify the behavior of classes. There are two ways we can use decorators with classes: (1) by decorating individual class methods or (2) by decorating the class ...
🌐
Python
peps.python.org › pep-0318
PEP 318 – Decorators for Functions and Methods | peps.python.org
A solution to this problem is to move the transformation of the method closer to the method’s own declaration. The intent of the new syntax is to replace · def foo(cls): pass foo = synchronized(lock)(foo) foo = classmethod(foo) with an alternative that places the decoration in the function’s declaration:
🌐
Dive into Python
diveintopython.org › home › learn python programming › classes in python › class decorator
Decorators for Class Methods and Properties in Python - Guide with Examples
May 3, 2024 - Class method decorator: A Python decorator that modifies a class method by changing its behavior or adding additional functionality.
Find elsewhere
🌐
Real Python
realpython.com › primer-on-python-decorators
Primer on Python Decorators – Real Python
September 1, 2026 - The @classmethod and @staticmethod decorators are used to define methods inside a class namespace that aren’t connected to a particular instance of that class. The @property decorator is used to customize getters and setters for class attributes.
🌐
Dontusethiscode
dontusethiscode.com › blog › py-decorators-adv.html
Python: Advanced Decorators
A class decorator can be written the exact same way as a function decorator- just know that the object that is passed into your decorating function will be the decorated class instead of a function. We can manipulate the passed class in any manner that Python allows us to- including the dynamic ...
🌐
Medium
medium.com › @jaywang.recsys › python-decorator-3a367a53df50
Python Decorator. In Python, decorators are a powerful… | by Jay Wang | Medium
November 12, 2024 - Class Method Decorator (@classmethod): Converts a method to a class method, so it receives the class (cls) as the first argument instead of the instance (self).
🌐
GeeksforGeeks
geeksforgeeks.org › python › class-as-decorator-in-python
Class as Decorator in Python - GeeksforGeeks
July 11, 2025 - While function-based decorators ... to function properly. A Python class decorator is simply a class that implements the __call__ method, allowing an instance of the class to be used as a decorator....
🌐
Sentry
sentry.io › sentry answers › python › difference between `@staticmethod` and `@classmethod` function decorators in python
Python @staticmethod vs @classmethod Decorators Explained | Sentry
1 week ago - By contrast, the @classmethod decorator will make Python pass the class of the instance it’s called on as the first argument. By convention, the first parameter of a class method is called cls.
🌐
Towards Data Science
towardsdatascience.com › home › latest › decorating methods defined in a class with python
Decorating Methods defined in a Class With Python | Towards Data Science
March 5, 2025 - To provide data integrity, I will write a decorator, named interger_check. To decorate a method in a class, first use the '@' symbol followed by the name of the decorator function.
🌐
GeeksforGeeks
geeksforgeeks.org › python › decorators-in-python
Decorators in Python - GeeksforGeeks
Calling obj.say_hello() say_hello method is now wrapped with additional behavior. 3. Class Decorators: used to modify or enhance behavior of a class. Like function decorators, class decorators are applied to class definition.
Published: 3 weeks ago
🌐
Readthedocs
jfine-python-classes.readthedocs.io › en › latest › decorators.html
Decorators — Objects and classes in Python tutorial
The decorator syntax for a class statement is same, except of course that it applies to a class statement. Unless you tell it not to, Python will create what is called a bound method when a function is an attribute of a class and you access it via an instance of a class.
🌐
DataCamp
datacamp.com › tutorial › decorators-python
Python Decorators Explained: Function and Class-Based Examples | DataCamp
February 5, 2026 - While function-based decorators ... will help you understand this section better.A class-based decorator is a class with a __call__ method that allows it to behave like a function....
🌐
Reddit
reddit.com › r/learnpython › decorators and class methods
r/learnpython on Reddit: Decorators and class methods
April 16, 2024 -

I could write my class like this:

class Fnord():
    def __init__(self, bar:str):
        self._bar = bar

    @property
    def bar(self) -> str:
        return self._bar

    @property
    def BAR(self) -> str:
        return self.bar

But this feels a little verbose. This feels (to me, anyway) that it ought to be possible to achieve the same end with another decorator:

class Fnord():
    # init method as above

    @property_alias("BAR")
    @property
    def bar(self) -> str:
        return self._bar

I've spent a lot of time reading about decorators and am thoroughly confused. Any help is appreciated.

🌐
Bas
bas.codes › posts › python-decorators
Understanding Decorators in Python - Bas codes
February 23, 2022 - Remember that the @ notation is ... the timer is only executed at the line my_obj = MyClass(). Class methods are not automatically decorated when decorating a class....
🌐
GeeksforGeeks
geeksforgeeks.org › creating-decorator-inside-a-class-in-python
Creating Decorator inside a class in Python | GeeksforGeeks
September 5, 2020 - Example 1: Here in this example we are creating a decorator function inside Class A. Inside Class A "fun1" Instance Method is calling the decorator function "Decorators" inside Class B "fun2". Instance Method is calling the decorator function of Class A.