If your program needs to define a new kind of values, use a class. Follow the below steps to define a class.
- Come up with a good name for the type.
- Determine the components and their types.
- Write a
__init__
function that initializes the components of the type appropriately from the arguments. This may involve complex logic.
To write functions that operate on your new type, follow the same instructions as for tuples. Instead of unpacking, you may use .
to access the components.
-
Write a class to represent the shape circle. Define operations to compute the area and perimeter on values of this new type.
-
Change the definition of
Circle
so that it now represents circles in the two-dimensional plane. i.e., it should now incorporate origin into the definition. -
Write a function that takes two circles as input and determines whether they overlap. What is the appropriate return type? How can you determine whether two circles overlap?
-
Write a function to create a
Point
from polar co-ordinates. -
Define an operation to add two
Rational
numbers.