2012-06-26 53 views
0

這是我的腳本:#錯誤:類型錯誤:不受約束的方法__init __()必須與形狀例如被稱爲第一個參數(有代替STR實例)#

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() 

我的錯誤:

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

我不明白。 爲什麼我得到這個錯誤信息? 解決方案是什麼? 爲什麼?

謝謝!

回答

1

你的代碼的工作版本,我在評論

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() 
+0

縮進會導致IndentationError,而不是TypeError。錯誤縮進幾乎肯定是複製/粘貼到SO中的結果。 – mgilson

+0

還有其他錯誤,比如'self'不會傳遞給'__init__',他在print語句中只使用'radius'而不是'self.radius'。 –

+0

非常感謝您 – Oldran

0

一般解釋了錯誤,你應該張貼的回溯。它使事情更容易調試。問題(比我假設從複製/粘貼錯誤附帶的壓痕等)是當你撥打:

shape.__init__(name) 

當你從形狀繼承。

如果你看shape.__init__的「原型」,它看起來像shape.__init__(self,name) - 所以這就是你應該使用的。錯誤來了,因爲你傳遞name(字符串),你應該通過self(一PolyCube,因此也shape由於繼承)

ASIDE

此外,在python 2.X ,總是從object繼承是一個好習慣。例如:

class shape(object): 
    ... 

這允許你使用與新風格類相關的所有優點。 (在Python 3中,所有類都是新類)

+0

感謝您的解釋 – Oldran

相關問題