0

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.

Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
  • 4
    Put the logic in a method instead of the constructor. Then only call the constructor if needed. – Jorn Mar 08 '23 at 17:24
  • 1
    E.g. a classmethod, `Apple.from_maybe_apple(unknown_thing)`. That's the convention for alternative constructors, see e.g. https://stackoverflow.com/a/682545/3001761. But it's unclear how any of that relates to the _function_ you're describing, why is what you've put in `__init__` not just in that function? – jonrsharpe Mar 08 '23 at 17:28
  • btw your `__init__` method cannot work how you would like. It will produce this error: `TypeError: __init__() should return None, not 'Apple'` – quamrana Mar 08 '23 at 17:28

1 Answers1

2

Just handle the logic in a function:

def parse_data(data):
    if isinstance(data, ParsedData):
        return data
    return ParsedData(data)

or as others have suggested, you can use a classmethod

class ParsedData:
    def __init__(self, data):
        ...

    @classmethod
    def from_data(cls, data):
        if isinstance(data, cls):
            return data
        return cls(data)  


parsed = ParsedData.from_data(my_data)
jprebys
  • 2,469
  • 1
  • 11
  • 16