[英]# 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. 如果您查看shape.__init__
的“原型”,它看起来就像shape.__init__(self,name)
-这就是您应该使用的。 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) 错误即将到来是因为您要传递name
(字符串),而应该传递self
( PolyCube
,因此也应继承shape
)
ASIDE 在旁边
Also, in python 2.x, it is good practice to always inherit from object
. 另外,在python 2.x中,最好始终从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) (在python 3中,所有类都是新样式类)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.