> Foo.get_name
    <bound method Foo.get_name of <class '__main__.Foo'>>
    >>> Foo.__dict__['get_name'].__get__(None, Foo)
    <bound method Foo.get_name of <class '__main__.Foo'>>
    

` __get__() ` gets called with two parameters – the object instance the
attribute belongs to ( ` None ` here, because accessing the attribute through
a class), and the owner class, i.e. the one the descriptor is defined on ( `
Foo ` in this case)  1  .

What the classmethod descriptor does is binding the original ` get_name() `
function to its class ( ` Foo ` ), and returning a bound method object. When
the latter gets called, it invokes ` get_name() ` , passing class ` Foo ` as
the first argument ( ` cls ` ) along with any other arguments the bound method
was originally called with.

Armed with this knowledge it is now clear why our ` logged ` decorator from
the beginning does not always work. It assumes that the object passed to it is
directly callable, and does not take the descriptor protocol into account.

#####  Making it right

Describing how to adjust the ` logged ` decorator to work correctly is quite a
lengthy topic, and out of scope of this post. If interested, you should
definitely read the [ blog series
](https://github.com/GrahamDumpleton/wrapt/tree/master/blog) by Graham
Dumpleton, as it addresses many more aspects than just working well with
classmethods. Or just use his [ wrapt ](https://pypi.python.org/pypi/wrapt)
library for writing decorators:

    
    
    import wrapt
    
    @wrapt.decorator
    def logged(wrapped, instance, args, kwargs):
        print('Invoking', wrapped)
        return wrapped(*args, **kwargs)
    
    class Foo(object):
        @logged
        @classmethod
        def get_name(cls):
            return cls.__name__
    
    >>> Foo.get_name()
    Invoking <bound method Foo.get_name of <class 'main2.Foo'>>
    'Foo'
    

Yup, it works.

* * *

  1. On the other hand, if retrieving a descriptor object directly from the class’s ` __dict__ ` , the descriptor’s ` __get__() ` method is bypassed, and that’s why we used ` Foo.__dict__['get_name'] ` at a few places in the examples.  ↩ 

###  Share this:

  * [ Twitter  ](https://blog.peterlamut.com/2017/10/17/applying-a-decorator-to-a-class-method-results-in-an-error/?share=twitter "Click to share on Twitter")
  * [ Facebook  ](https://blog.peterlamut.com/2017/10/17/applying-a-decorator-to-a-class-method-results-in-an-error/?share=facebook "Click to share on Facebook")
  * 

Like  Loading...

![](https://1.gravatar.com/avatar/47889a61e55c396e2ea18b0de05fa8a63f36cf32eddcf552613be0ebca37fb83?s=49&d=identicon&r=G)
Author  [ Peter Lamut ](https://blog.peterlamut.com/author/peterlamut/) Posted
on  [ October 17, 2017  October 17, 2017
](https://blog.peterlamut.com/2017/10/17/applying-a-decorator-to-a-class-
method-results-in-an-error/) Tags  [ Classmethod
](https://blog.peterlamut.com/tag/classmethod/) , [ Decorators
](https://blog.peterlamut.com/tag/decorators/) , [ Descriptors
](https://blog.peterlamut.com/tag/descriptors/) , [ Python
](https://blog.peterlamut.com/tag/python/) [ Leave a comment  on Applying a
decorator to a class method results in an error
](https://blog.peterlamut.com/2017/10/17/applying-a-decorator-to-a-class-
method-results-in-an-error/#respond)

##  About the Author

![](https://1.gravatar.com/avatar/47889a61e55c396e2ea18b0de05fa8a63f36cf32eddcf552613be0ebca37fb83?s=96&d=identicon&r=G)  
I am an experienced software engineer, primarily using Python and JavaScript
in my day-to-day work, putting a strong emphasis on writing clean and
maintainable code.

I use Linux, like to crack jokes, and enjoy attending developer meetups and
conferences like EuroPython.

I do not own any pets or hair care products.

Search for:  Search

##  Tags

  * [ Attribute lookup ](https://blog.peterlamut.com/tag/attribute-lookup/)
  * [ ChainMap ](https://blog.peterlamut.com/tag/chainmap/)
  * [ Classmethod ](https://blog.peterlamut.com/tag/classmethod/)
  * [ CPy