Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
I am currently learning Python, from that, I got to know about method decorators. I want to ask that can we decorate the classes?

1 Answer

0 votes
by (108k points)

Yes, you can easily decorate the classes, it is just a method that takes some arguments:

That argument can be anything, it can be the class as well.

#!/usr/bin/env python3

def decorate(cls):

    print(cls)

    return cls

@decorate

class Foo: pass

$ python example.py

__main__.Foo

$ python3 example.py

<class '__main__.Foo'>

The class decorator will return an arbitrary object as the output.

If you want to know more about Python, the do refer to the Python certification course.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 9, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...