Posts

Showing posts with the label methods

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 ...

How to return multiple types of data in C#

How to return multiple types of data in C# I do return different instances of service depending on a enum in my application: public (????) CurrentService { get { switch (CurrentServiceEnum) { case ServiceType.ServiceA: return IoC.ServiceA; case ServiceType.ServiceB: return IoC.ServiceB; case ServiceType.ServiceC: return IoC.ServiceC; } } } Each of that service has it's own implementation of RemoveAccount method, taking different type argument. RemoveAccount internal class ServiceA_AccountDataModel { [..] } internal class ServiceB_AccountDataModel { [..] } internal class ServiceC_AccountDataModel { [..] } public class ServiceA { public void RemoveAccount(ServiceA_AccountDataModel model) { [...] } } public class ServiceB { public void RemoveAccount(ServiceB_AccountDataModel model) { [...] } } public class ServiceC { public void RemoveAccount(ServiceC_A...