0

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?

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • Post a reproducible minimum example. I guess you are querying from the type in some cases and from an instance in other. – Paulo Scardine Oct 21 '13 at 23:10
  • Works fine here too. What does `dir(my_class)` say? – flup Oct 21 '13 at 23:15
  • OK, I;m new to python classes, so excuse the mistake. Where my methods are unbound, I have *instance* methods of the same name. I figured that python would be able to tell the difference between my_class.method() and my_instance.method(). Guess I was wrong. I haven't actually seen this written anywhere. Can someone point me to it? Thanks. – bob.sacamento Oct 21 '13 at 23:15
  • I guess you are used to a language with function/method overloads... – Paulo Scardine Oct 21 '13 at 23:20

1 Answers1

3

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
Murph
  • 1,479
  • 2
  • 13
  • 26