0

I've just started learning python and have recently encountered a problem while learning about classes. please have a look at the code.

class Critter(object):
    """your very own bug generator"""
    total=0
    def status(x):
        print Critter.total
        status=staticmethod(status)
    def __init__(self,name):
        print'a critter has been created'
        self.name=name
        Critter.total+=1


crit1=Critter('pooch')
crit2=Critter('Duff')
crit3=Critter('pluto')

Critter.status()
print'\nAccessing the class attributes through an object:',crit1.total

upon running the code I get this error:

line 19, in <module>
Critter.status(Critter.total)
TypeError: unbound method status() must be called with Critter instance as first  
argument(got int instance instead)

I'm still unclear about how bound/unbound works. Sorry for the beginner question, any help given would be greatly appreciated.

mgilson
  • 300,191
  • 65
  • 633
  • 696
user2197773
  • 19
  • 1
  • 3

2 Answers2

1

Your code had the following problems:

  • indentation issues. Any variables or methods you define that needs to be bound to a class (i.e. belongs to the class) need to be indented one level under the class.
  • The status=staticmethod(status) needs to be directly defined under the class and not in the scope of the status() method's definition. Since status refers to Critter.status()
  • The status() method should not be taking any parameter x at least based on the Critter.status() call you've showed.

The above error you see indicates that python did not recognize the staticmethod call in the definition of your class, since its indentation was not correct. So python just defined the status() method as a mere instance method (which is the default). For instance methods, python expects the first parameter to be the instance handle.

This should work:

class Critter(object):
    """your very own bug generator"""
    total=0

    def status():
        print Critter.total
    status=staticmethod(status)

    def __init__(self,name):
        print 'a critter has been created'
        self.name=name
        Critter.total+=1

crit1=Critter('pooch')
crit2=Critter('Duff')
crit3=Critter('pluto')

Critter.status()
print'\nAccessing the class attributes through an object:',crit1.total

Output:

a critter has been created
a critter has been created
a critter has been created
3

Accessing the class attributes through an object: 3

If you're using python > 2.4 (which is likely the case), you can make use of the @staticmethod decorator instead to define static methods as follows:

class Critter(object):
    """your very own bug generator"""
    total=0

    @staticmethod
    def status():
        print Critter.total

    def __init__(self,name):
        print 'a critter has been created'
        self.name=name
        Critter.total+=1
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
0

Chnage your code to:

class Critter(object):
    """your very own bug generator"""

    total = 0

    @staticmethod
    def status():
        print Critter.total

    def __init__(self,name):
        print 'a critter has been created'
        self.name = name
        Critter.total += 1

To find more on bound and unbound methods try to read this Class method differences in Python: bound, unbound and static

Community
  • 1
  • 1
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
  • I don't think this is what OP needs. What is the `status = staticmethod(status)` trying to accomplish here? – mgilson Mar 22 '13 at 04:18
  • I don't know why someone's downvoting these. Perhaps they feel like a classmethod would be more appropriate? but I don't think you can make that judgment here ... – mgilson Mar 22 '13 at 04:23
  • Thanks for the link! i'm reading up on it now – user2197773 Mar 22 '13 at 05:41