Search code examples
pythonclass-method

Why are my class methods unbound?


Here's a snippet of a python class I am working on. (Sorry I can't go into too much detail. My organization gets a little antsy about such things.

class my_class:

    @classmethod
    def make_time_string(cls, **kwargs):
    # does some stuff

    @classmethod
    def make_title(cls, **kwargs):
    # does some other stuff

    @classmethod
    def give_local_time(cls, **kwargs):
    # yet more stuff

    @classmethod
    def data_count(cls, **kwargs):
    # you guessed it

So far, so good, I should think. But I am having trouble using this class. And it turns out that the reason is that the last two methods are unbound:

>>> my_class.make_time_string
<bound method classobj.make_time_string of <class __main__.my_class at 0x1cb5d738>>
>>> my_class.make_title
<bound method classobj.make_title of <class __main__.my_class at 0x1cb5d738>>
>>> my_class.give_local_time
<unbound method my_class.give_local_time>
>>> my_class.data_count
<unbound method my_class.data_count>

Sorry, again, that I can't be much more forthcoming about the contents. But can anyone think of a reason why those last two methods should be unbound while the first two are (correctly) bound?


Solution

  • I tried your code, it works for me.

    So, you'll need to provide a better example.

    My best guess is that you are somehow typo'ing @classmethod or are no longer in the class definition or something. Maybe you're overwriting the classmethod decorator?

    Here's my code:

    class my_class:
        @classmethod
        def make_time_string(cls, **kwargs):
            pass
    
        @classmethod
        def make_title(cls, **kwargs):
            pass
    
        @classmethod
        def give_local_time(cls, **kwargs):
            pass
    
        @classmethod
        def data_count(cls, **kwargs):
            pass
    
    print my_class.make_time_string
    print my_class.make_title
    print my_class.give_local_time
    print my_class.data_count