0

Original class program:

 from Circle import circle
 class circle:
    def __init__(self,radius=1):  #write def __init__(self,radius=1) to set a value
        self.radius=radius
    # constructer constructs the object and initializes it

    def getArea(self):
        return(3.142*self.radius*self.radius)

    def getPerimeter(self):
        return(2*3.142*self.myradius)

Using class:

def main():
    c1=circle()
    #If below happens
    c1.radius=-1
    #if above happens then negative value will be returned
    c2=circle(5)
    c3=circle(3)
    print(c1.getArea())
    print(c2.getArea())
    print(c3.getArea())

main()

I was just trying to learn about classes in python. When I run the program it says that

builtins.AttributeError: 'circle' object has no attribute 'getArea'

I am not able to understand why it is happening.

9
  • 7
    Are you certain your indentation is correct? I corrected indentation in this post assuming that you just had trouble with how to post, but it could be that the getArea() method is not seen as part of the circle class. Also, why are you importing Circle.circle then overriding it with a new class? Commented Apr 1, 2014 at 16:19
  • 3
    Why are you importing circle and the re-defining it? Commented Apr 1, 2014 at 16:20
  • 1
    Apart from the strange import on top, your code seems to run perfectly. Note that indentation is very important in python. A stray space or confusing tabs and spaces will make your code behave incorrectly. Commented Apr 1, 2014 at 16:22
  • 1
    from Circle import circle is useless if you then immediately redefine circle. This cannot be how you were taught to do things. Commented Apr 1, 2014 at 16:32
  • 1
    Tell whomever you are learning from that the Interwebs says they are wrong. Yes, Interwebs. Commented Apr 1, 2014 at 16:58

2 Answers 2

2

What about something like this:

from math import pi

class Circle:
    def __init__(self,radius=1):
        self.radius=radius

    def get_area(self):
        return pi * self.radius**2

    def get_circumvention(self):
        return 2 * pi * self.radius


if __name__ == "__main__":
    c1=Circle()
    #If below happens
    c1.radius=-1
    #if above happens then negative value will be returned
    c2=Circle(5)
    c3=Circle(3)
    print(c1.get_area())
    print(c2.get_area())
    print(c3.get_area())
    print(c1.get_circumvention())
    print(c2.get_circumvention())
    print(c3.get_circumvention())

The error you are seeing is probably because you have that weird import statement on top that hides your circle class (you try to call Circle.circle.getArea() that does not exists)

Furthermore:

  • python coded styles suggest CapsWords for class names and lowercase for methods and functions.
  • self.myradius is not defined in __init__ so getPerimeter will fail.
  • The brackets in the methods are not necessary.
  • math has pi
  • To the power is noted as ** (5**2 == 25)
  • Use the if __name__ == "__main__": construct if you only want to execute when directly run and not on import.
  • Perimeter -> circumvention
  • Use the import statement in another python file that you want to use the Circle class in: from whateveryounamedthisfile import Circle. Then you can use Circle like you would in this file.
Sign up to request clarification or add additional context in comments.

Comments

0

Your import is pointless. Don't import 'circle' from 'Circle' if you, as the comments have stated. Also, you have not defined the 'myradius' attribute, therefore your 'getPerimeter()' function will not work unless you change that.

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.