Python #错误:TypeError:必须以shape实例作为第一个参数调用未绑定的方法_uinit__;()(改为获取str实例)#
这是我的剧本:Python #错误:TypeError:必须以shape实例作为第一个参数调用未绑定的方法_uinit__;()(改为获取str实例)#,python,inheritance,Python,Inheritance,这是我的剧本: 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.printMys
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) #
我不明白。
为什么我会收到这个错误消息?
解决办法是什么?
为什么
谢谢 作为代码的工作版本,我已经在注释中解释了错误
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()
通常,您应该发布完整的回溯。它使调试变得容易得多。问题(除了缩进,我认为缩进来自复制/粘贴错误)在于调用:
shape.__init__(name)
当您从形状继承时
如果你看shape.\uuuu init\uuuuu
的“原型”,它看起来像shape.\uuuuu init\uuuuu(self,name)
——这就是你应该使用的。出现错误是因为您正在传递name
(一个字符串),而您应该传递self
(一个PolyCube
,因此也是一个shape
,因为继承)
旁白
另外,在Python2.x中,最好始终从对象继承。e、 g:
class shape(object):
...
这允许您使用与新样式类关联的所有优点。(在Python3中,所有类都是新样式的类)缩进将导致缩进错误,在本例中不是TypeError。错误的缩进几乎可以肯定是复制/粘贴到SO中的结果。还有其他错误,例如self
没有传递到\uuuu init\uuuuu
,他在print语句中只使用radius
而不是self.radius
。