How would we create a class constructor which does nothing if the input is already an instance of that class?
The following code does not work, but it does partially show you what I am trying to accomplish:
class Apple:
def __init__(self, *args):
if len(args) == 1 and isinstance(other, type(self)):
return self
else:
self._seeds = "".join(str(arg) for arg in args)
We probably have to override (overload?) __new__
, or create a meta-class, but I am not sure how to do that.
The class named Apple
was very contrived.
In the big picture, I am trying to write a function which can accept either parsed or un-parsed data as input.
If the data is already parsed, there is no reason to parse it a second time.
If the input is an instance of ParsedData
, then we return the input.
If the input is not an instance of ParsedData
then we pass the input into the constructor of the class named ParsedData
.