简体   繁体   English

错误:TypeError:未绑定方法__init __()必须以shape实例作为第一个参数调用(代替str实例)

[英]# 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 (字符串),而应该传递selfPolyCube ,因此也应继承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.

相关问题 TypeError:未绑定方法__init __()必须以薪资实例作为第一个参数来调用(取而代之的是int实例) - TypeError: unbound method __init__() must be called with payroll instance as first argument (got int instance instead) 错误消息:“ TypeError:必须使用日志实例作为第一个参数来调用未绑定方法w()(而是使用got str实例)” - Error message: “TypeError: unbound method w() must be called with log instance as first argument (got str instance instead)” 未绑定方法__init __()必须以CreditCard实例作为第一个参数调用(取而代之的是VisaCreditCard实例) - unbound method __init__() must be called with CreditCard instance as first argument (got VisaCreditCard instance instead) 必须使用LocaleRegexProvider实例作为第一个参数调用未绑定方法__init __()(改为使用RegexURLPattern实例) - unbound method __init__() must be called with LocaleRegexProvider instance as first argument (got RegexURLPattern instance instead) Python __init__问题:必须以Bank实例作为第一个参数来调用未绑定方法__init __()(改为使用int实例) - Python __init__ issue: unbound method __init__() must be called with Bank instance as first argument (got int instance instead) TypeError:必须使用“Class name”实例作为第一个参数调用unbound方法“method name”(改为使用str实例) - TypeError: unbound method “method name” must be called with “Class name” instance as first argument (got str instance instead) TypeError:未绑定方法“方法名称”必须以“类名称”实例作为第一个参数来调用(取而代之的是str实例) - TypeError: unbound method 'method name' must be called with 'class name' instance as first argument (got str instance instead) TypeError:未绑定方法sadd()必须以StrictRedis实例作为第一个参数来调用(取而代之的是str实例) - TypeError: unbound method sadd() must be called with StrictRedis instance as first argument (got str instance instead) TypeError:未绑定的方法player_init()必须以Player实例作为第一个参数调用(取而代之的是int实例) - TypeError: unbound method player_init() must be called with Player instance as first argument (got int instance instead) 类型错误:必须在 Form1 的第 38 行使用日期时间实例作为第一个参数调用未绑定的方法 strftime()(改为使用 str 实例) - TypeError: unbound method strftime() must be called with datetime instance as first argument (got str instance instead) at Form1, line 38
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM