Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Constructing a Circle From Its Diameter

00:01 Constructing a Circle From Its Diameter. To create your first class constructor with the @classmethod decorator, let’s say you are coding a geometry-related application and need a Circle class.

00:15 Initially, you define your class as seen on-screen.

00:23 The initializer of Circle takes a radius value as an argument and stores it in an instance attribute called .radius. Then the class implements methods to compute the circle’s area and perimeter using Python’s math module.

00:50 The special method .__repr__() returns a suitable string representation for your class. Save in your working directory, and open your Python interpreter and run the code seen on-screen to try out Circle.

01:25 This looks good. Your class works correctly. Now, let’s say that you also want to instantiate Circle using the diameter. Then you could use code similar to what’s seen on-screen, but that’s not quite Pythonic or intuitive.

01:42 It would be better to have an alternative constructor to create circles by using their diameter directly. Go ahead and add the following class method to Circle right after .__init__().

02:01 Here, you define .from_diameter() as a class method. The name follows the popular convention within the Python community using the from preposition to name constructors that you create as class methods.

02:14 The first argument receives a reference to the containing class, Circle. The second argument holds the diameter of the specific circle that you want to create. Inside the method, you first calculate the radius using the input value of diameter.

02:29 Then you instantiate Circle by calling cls with the radius that results from the diameter argument. This way, you are in full control of creating and initializing the instances of Circle using the diameter as an argument.

02:43 The call to the cls argument automatically runs the object creation and initialization steps that Python needs to instantiate a class. Finally, .from_diameter() returns the new instance to the caller. In this example, you can seemingly achieve the same result by calling Circle itself instead of cls. However, this can potentially lead to bugs if your class is subclassed.

03:07 Those subclass would then call Circle instead of themselves when they’re initialized with .from_diameter(). On-screen, you can see how to use your brand-new constructor to create circles by using the diameter.

03:26 The call to .from_diameter() on Circle returns a new instance of the class. To construct that instance, the method uses the diameter instead of the radius.

03:37 Note that the rest of the functionality of Circle works the same as before. Using the @classmethod decorator as you did here is the most common way to provide explicit multiple constructors in your classes. With this technique, you have the option to select the right name for each alternative constructor that you provide, which can make your code more readable and maintainable.

04:02 In the next section of the course, you’ll take a look at a more elaborate example of providing multiple constructors.

Become a Member to join the conversation.