Python instance XX has no YY attribute

I have this noob error,

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

      

here's my code:

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 

      

+3


source to share


3 answers


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

        def multiplicaMethod (self):

      

The latter def

is indented incorrectly. Deliver him so that he is at the same level as def __init__(self, a):

, for example:



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'

      

Also note that your code is using classic classes . You probably don't want this, but this is a simple fix - just inherit from object

.

+3


source


yours is multiplicaMethod()

defined inside the method __init__

. he has stepped back too far. move it to the left to be inside a class instead.



+3


source


The multipicaMethod function should be deferred to the left. It is now a local function inside init .

+1


source







All Articles