Building a Polar Point From Cartesian Coordinates
00:00 Building a Polar Point From Cartesian Coordinates. For a more elaborate example of providing multiple constructors using class methods, let’s say you have a class representing a polar point in a math-related application.
00:15 You need a way to make your class more flexible so that you can construct new instances using Cartesian coordinates as well. On-screen, you can see how to write a constructor to meet this requirement.
00:50
.from_cartesian()
takes two arguments representing a given point’s x
and y
Cartesian coordinates. Then the method calculates the required distance
and angle
to construct the corresponding PolarPoint
object.
01:08
Finally, .from_cartesian()
returns a new instance of the class.
01:36 On-screen, you can see the class working using both coordinate systems. First you create a point using polar coordinates, and then one using Cartesian coordinates.
01:55
In these examples, you use the standard instantiation process and your alternative constructor, .from_cartesian()
, to create PolarPoint
instances using conceptually different initialization arguments.
02:10 In the next section of the course, you’ll take a closer look at the use of multiple constructors in Python’s built-in and standard-library classes.
Become a Member to join the conversation.