Let’s consider a class that makes it possible to create triangle instances based on the lengths of their three sides. In this case, the __init__ method accepts three parameters, one for each side length.
However, a triangle can also be defined in other ways, as shown below: by one side and the two adjacent angles, or by two sides and the angle between them.

So how can you create triangles from these alternative sets of data using the same class?
You can achieve this using class methods. Since a class method has access to the class object itself, it can not only interact with class-level attributes but also instantiate new objects within the method. The return value of such a method can therefore be a new instance of the class. In essence, this allows you to define alternative constructors — commonly referred to as factory methods — which produce instances of the class.
In the following code snippet, you can see a Triangle class that includes two factory methods supporting these alternative ways of creating triangles. The side lengths are calculated using the Law of Sines and the Law of Cosines from trigonometry.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
from math import sin, cos, radians class Triangle: def __init__(self, a, b, c): self.a, self.b, self.c = a, b, c def __repr__(self): return f'{type(self).__name__}({self.a:.2f}, {self.b:.2f}, {self.c:.2f})' @classmethod def from_one_side_and_two_angles(cls, a, beta, gamma): """ Creates a triangle given two angles and one side, which is the side adjacent to the two given angles (in degrees). Calculates the remaining sides using the Law of Sines, then returns a new instance. """ beta_rad, gamma_rad, alpha_rad = radians(beta), radians(gamma), radians(180 - beta - gamma) v = a / sin(alpha_rad) b, c = v * sin(beta_rad), v * sin(gamma_rad) return cls(a, b, c) @classmethod def from_two_sides_and_included_angle(cls, a, b, gamma): """ Creates a triangle given two sides and the angle between them (in degrees). Calculates the third side using the Law of Cosines, then returns a new instance. """ gamma_rad = radians(gamma) c = (a ** 2 + b ** 2 - 2 * a * b * cos(gamma_rad)) ** 0.5 return cls(a, b, c) # TEST t1 = Triangle(4, 3, 5) print(t1) # Output: Triangle(4.00, 3.00, 5.00) t2 = Triangle.from_one_side_and_two_angles(4, 36.87, 90) print(t2) # Output: Triangle(4.00, 3.00, 5.00) t3 = Triangle.from_two_sides_and_included_angle(4, 3, 90) print(t3) # Output: Triangle(4.00, 3.00, 5.00) t4 = Triangle(10, 10, 10) print(t4) # Output: Triangle(10.00, 10.00, 10.00) t5 = Triangle.from_one_side_and_two_angles(10, 60, 60) print(t5) # Output: Triangle(10.00, 10.00, 10.00) t6 = Triangle.from_two_sides_and_included_angle(10, 10, 60) print(t6) # Output: Triangle(10.00, 10.00, 10.00) |
You can read more about class methods in the chapter „Types and classes – implementing types with classes” in the e-book Python Knowledge Building Step by Step.