Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A circle class
- class Circle():
- """A Circle Class"""
- # pi which is common to all circle objects.
- pi = 3.14
- # Constructor
- def __init__(self, radius):
- """Circle constructor"""
- # Radius of the circle
- self.radius = radius
- # Area of the circle
- self.area = (Circle.pi * (radius ** 2))
- # Circumference of the circle
- self.circumference = (2 * Circle.pi * radius)
- # Create a first instance of the circle
- circle01 = Circle(4)
- # Access each attribute of the circle
- print("Area = {}, Circumference = {}, Radius = {}".format(circle01.area, circle01.circumference, circle01.radius))
- # Now try to see all the attributes of a circle
- print(dir(circle01))
- # The following was the output for the above statement
- # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
- # '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
- # '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
- # '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
- # '__str__', '__subclasshook__', '__weakref__', 'area', 'circumference', 'pi',
- # 'radius']
- # Now try to set color to the circle01
- circle01.color = "Red"
- print("Color of circle01 is {}".format(circle01.color))
- # Now try to see all the attributes of a circle
- print(dir(circle01))
- # The following is the output of the above print statement, where a new attribute
- # color is added to the object 'circle01'
- # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
- # '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
- # '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
- # '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
- # '__str__', '__subclasshook__', '__weakref__', 'area', 'circumference', 'color',
- # 'pi', 'radius']
Add Comment
Please, Sign In to add comment