简体   繁体   中英

# Error: TypeError: unbound method __init__() must be called with shape instance as first argument (got str instance instead) #

It's my script:

class shape:
    def __init__(self, name):
        self.name = name

    def printMyself(self):
        print 'I am a shape named %s.' % self.name

shape1 = shape(name = 'myFirstShape.')
shape2 = shape(name = 'mySecondShape.')
shape1.printMyself()
shape2.printMyself()


class polyCube(shape):
    def __init__(self, name, length, width, height):
        shape.__init__(name)
        self.length = length
        self.width = width
        self.height = height

    def printMyself(self):
        shape.printMyself(self)
        print 'I am also a cube with dimensions %.2f, %.2f, %.2f.' % (length, width, height)


class polySphere(shape):
    def __init__(self, name, radius):
        shape.__init__(name)
        self.radius = radius

    def printMyself(self):
        shape.printMyself(self)
        print 'I am also a sphere with dimensions of %.2f.' % (radius)

cube1 = polyCube('firstCube', 2.0, 1.0, 3.0)
cube2 = polyCube('secondCube', 3.0, 3.0, 3.0)
sphere1 = polySphere('firstSphere', 2.2)
sphere2 = polySphere('secondSphere', 3.5)
shape1 = shape('myShape')
cube1.printMyself()
cube2.printMyself()
sphere1.printMyself()
sphere2.printMyself()

My error:

# Error: TypeError: unbound method __init__() must be called with shape instance as first argument (got str instance instead) # 

I don't understand. Why I got this error message? What's the solution? And why?

Thanks!

A working version of your code, i've explained the errors in comments

class shape:
  def __init__(self, name):
    self.name = name

  def printMyself(self):
    print ('I am a shape named %s.' % self.name)

shape1 = shape(name = 'myFirstShape.')
shape2 = shape(name = 'mySecondShape.')
shape1.printMyself()
shape2.printMyself()


class polyCube(shape):
    def __init__(self, name, length, width, height):
        shape.__init__(self,name) #pass self here, you're calling parent's __init__() explicitly so you should pass self.

        self.length = length
        self.width = width
        self.height = height

    def printMyself(self):
     shape.printMyself(self)
     #use self.length ,self.width instead of just length,width etc
     print ('I am also a cube with dimensions %.2f, %.2f, %.2f.' % (self.length, self.width, self.height)) 


class polySphere(shape):
    def __init__(self, name, radius):
        shape.__init__(self,name) #pass self here

        self.radius = radius

    def printMyself(self):
     shape.printMyself(self)
     print ('I am also a sphere with dimensions of %.2f.' % (self.radius)) #use self.radius here

cube1 = polyCube('firstCube', 2.0, 1.0, 3.0)
cube2 = polyCube('secondCube', 3.0, 3.0, 3.0)
sphere1 = polySphere('firstSphere', 2.2)
sphere2 = polySphere('secondSphere', 3.5)
shape1 = shape('myShape')
cube1.printMyself()
cube2.printMyself()
sphere1.printMyself()
sphere2.printMyself()

In general, you should post the full traceback. It makes things a lot easier to debug. The problem (other than the indentation which I assume came from copy/paste errors) is when you call:

shape.__init__(name)

when you inherit from shape.

If you look at shape.__init__ 's "prototype", it looks like shape.__init__(self,name) -- So that is what you should be using. The error is coming because you're passing name (a string) where you should be passing self (a PolyCube and therefore also a shape due to inheritance)

ASIDE

Also, in python 2.x, it is good practice to always inherit from object . eg:

class shape(object):
   ...

This allows you to use all the goodness associated with new-style classes. (in python 3, all classes are new-style classes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM