I'm trying to write my own "fraction" class that takes in two numerical objects: the numerator and denominator. However, depending on the data type for the two arguments, I'd like the object to initialize in different ways.
For example, if I declare x = Frac(2,5)
I want x
to stay a Frac
type. Whereas if I declare y = Frac(2.1, 5)
I want y
to be cast to a float
with the value of 0.42, rather than Frac(21,50)
.
What would be the right way to go about something like this?
This feels very similar to the scenario in which a tuple with a single object simply returns the original object. This means that x = ("cat")
sets x
to be a str
type, rather than a tuple
type.