Function to behave differently on class vs on instance
Function to behave differently on class vs on instance I'd like a particular function to be callable as a classmethod, and to behave differently when it's called on an instance. For example, if I have a class Thing , I want Thing.get_other_thing() to work, but also thing = Thing(); thing.get_other_thing() to behave differently. class Thing Thing.get_other_thing() thing = Thing(); thing.get_other_thing() I think overwriting the get_other_thing method on initialization should work (see below), but that seems a bit hacky. Is there a better way? get_other_thing class Thing: def __init__(self): self.get_other_thing = self._get_other_thing_inst() @classmethod def get_other_thing(cls): # do something... def _get_other_thing_inst(self): # do something else Isn't this what we use inheritance for. Define a separate class with your new method - which inherits from parent class, then by scope, instance will return ...
