Guest User

Untitled

a guest
Jul 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. # A circle class
  2. class Circle():
  3. """A Circle Class"""
  4. # pi which is common to all circle objects.
  5. pi = 3.14
  6.  
  7. # Constructor
  8. def __init__(self, radius):
  9. """Circle constructor"""
  10. # Radius of the circle
  11. self.radius = radius
  12.  
  13. # Area of the circle
  14. self.area = (Circle.pi * (radius ** 2))
  15.  
  16. # Circumference of the circle
  17. self.circumference = (2 * Circle.pi * radius)
  18.  
  19. # Create a first instance of the circle
  20. circle01 = Circle(4)
  21.  
  22. # Access each attribute of the circle
  23. print("Area = {}, Circumference = {}, Radius = {}".format(circle01.area, circle01.circumference, circle01.radius))
  24.  
  25. # Now try to see all the attributes of a circle
  26. print(dir(circle01))
  27.  
  28. # The following was the output for the above statement
  29.  
  30. # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
  31. # '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
  32. # '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
  33. # '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
  34. # '__str__', '__subclasshook__', '__weakref__', 'area', 'circumference', 'pi',
  35. # 'radius']
  36.  
  37. # Now try to set color to the circle01
  38. circle01.color = "Red"
  39.  
  40. print("Color of circle01 is {}".format(circle01.color))
  41.  
  42. # Now try to see all the attributes of a circle
  43. print(dir(circle01))
  44. # The following is the output of the above print statement, where a new attribute
  45. # color is added to the object 'circle01'
  46.  
  47. # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
  48. # '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
  49. # '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
  50. # '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
  51. # '__str__', '__subclasshook__', '__weakref__', 'area', 'circumference', 'color',
  52. # 'pi', 'radius']
Add Comment
Please, Sign In to add comment