gpt4 book ai didi

Python XX 实例没有属性 YY

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:22 26 4
gpt4 key购买 nike

我有这个菜鸟错误,

   l = instanciaHagale.multiplicaMethod() AttributeError: Hagale instance has no attribute 'multiplicaMethod'

这里是我的代码:

class Hagale :
def __init__(self, a):
self.a = a
print self.a

self.sumaleAlgo = self.a+34543 #variable creada on the fly!

def multiplicaMethod (self):

return 'self.cuadradoReal'
#self.cuadradoReal = self.a * 2

instanciaHagale = Hagale(345)

print instanciaHagale.sumaleAlgo #acceso a las variables de mi objeto!

l = instanciaHagale.multiplicaMethod()

print l

最佳答案

    def __init__(self, a):
# ...

def multiplicaMethod (self):

最后一个 def 缩进错误。缩小它,使其与 def __init__(self, a): 处于同一级别,如下所示:

class Hagale(object):
def __init__(self, a):
self.a = a
print self.a
self.sumaleAlgo = self.a+34543 #variable creada on the fly!

def multiplicaMethod (self): # <-- moved to the left
return 'self.cuadradoReal'

另请注意,您的代码使用了 classic classes .这可能不是您想要的,但它很容易解决 - 只需继承 object

关于Python XX 实例没有属性 YY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9786212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com