Using @classmethod for Multiple Class Constructors
The ultimate reason why you can use class methods as constructors is that you don’t need an instance to call a class method. Using the
@classmethod decorator makes it possible to add as many explicit constructors as you need to a given class.
@classmethod decorator returns the instance of the class. It’s a Pythonic and popular way to implement multiple constructors, and you can also call this type of constructor an alternative constructor.
Now, how can you use a class method to customize Python’s instantiation process? Instead of fine-tuning
.__init__() and the object initialization, you’ll control both steps: object creation and initialization. Through the following examples, you’ll learn how to do just that, starting with an example where you construct a circle from its diameter.
Become a Member to join the conversation.