3

I have this noob error,

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

here 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 Answers 3

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

        def multiplicaMethod (self):

The last def is indented wrong. Outdent it so it is at the same level as def __init__(self, a):, like this:

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 uses classic classes. That's probably not want you want, but it's an easy fix - simply inherit from object.

Sign up to request clarification or add additional context in comments.

Comments

3

your multiplicaMethod() is defined inside of the __init__ method. it's indented too far. move it to the left so that its inside the class instead.

Comments

1

The multiplicaMethod should be indented left. Now it is a local function inside init.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.