Pattern for 1) instantiating a class with defaults and 2) overriding some callback in instantiator /u/BenthicSessile Python Education

I have a class from an external library that I want to wrap in a class that provides it with default values and callback handlers, plus adding some extra methods, which seems easy enough. But I also want to be able to override some or all of the default callback handlers from the class that instantiates the (wrapped) library class. I’ve spent a fair amount of time looking for examples online but have not been able to find anything relevant, which makes me think I’ve misunderstood something fundamental. Nevertheless, I’ve managed to cook up this monstrosity, which does what I want:

class Thing(SuperThing): def __new__(self, caller, **kwargs): self.caller = caller self.some_default = "Some default value" return SuperThing( self.some_default, self.caller.callback_one if hasattr(self.caller,"callback_one") else self.callback_one, self.caller.callback_two if hasattr(self.caller,"callback_two") else self.callback_two ) def callback_one(self): print("The default callback_one handler") def callback_two(self): print("The default callback_two handler") class Other(): def some_method(self): thing = Thing(self) thing.do_it() def callback_one(self): print("This is an overridden callback_one handler") other = Other() other.some_method() 

“Other” is not related to “Thing” or “SuperThing” at all, but it does make sense for it to have the ability to provide its own callback handlers – and I want “Thing” to pass the call over to “Other” if it defines a matching handler. I’m sure this horrible pattern breaks many rules, and I would love to be told off for being an idiot, so although the pattern works I’d appreciate if you could tell me what’s wrong with it!

submitted by /u/BenthicSessile
[link] [comments]

​r/learnpython I have a class from an external library that I want to wrap in a class that provides it with default values and callback handlers, plus adding some extra methods, which seems easy enough. But I also want to be able to override some or all of the default callback handlers from the class that instantiates the (wrapped) library class. I’ve spent a fair amount of time looking for examples online but have not been able to find anything relevant, which makes me think I’ve misunderstood something fundamental. Nevertheless, I’ve managed to cook up this monstrosity, which does what I want: class Thing(SuperThing): def __new__(self, caller, **kwargs): self.caller = caller self.some_default = “Some default value” return SuperThing( self.some_default, self.caller.callback_one if hasattr(self.caller,”callback_one”) else self.callback_one, self.caller.callback_two if hasattr(self.caller,”callback_two”) else self.callback_two ) def callback_one(self): print(“The default callback_one handler”) def callback_two(self): print(“The default callback_two handler”) class Other(): def some_method(self): thing = Thing(self) thing.do_it() def callback_one(self): print(“This is an overridden callback_one handler”) other = Other() other.some_method() “Other” is not related to “Thing” or “SuperThing” at all, but it does make sense for it to have the ability to provide its own callback handlers – and I want “Thing” to pass the call over to “Other” if it defines a matching handler. I’m sure this horrible pattern breaks many rules, and I would love to be told off for being an idiot, so although the pattern works I’d appreciate if you could tell me what’s wrong with it! submitted by /u/BenthicSessile [link] [comments] 

I have a class from an external library that I want to wrap in a class that provides it with default values and callback handlers, plus adding some extra methods, which seems easy enough. But I also want to be able to override some or all of the default callback handlers from the class that instantiates the (wrapped) library class. I’ve spent a fair amount of time looking for examples online but have not been able to find anything relevant, which makes me think I’ve misunderstood something fundamental. Nevertheless, I’ve managed to cook up this monstrosity, which does what I want:

class Thing(SuperThing): def __new__(self, caller, **kwargs): self.caller = caller self.some_default = "Some default value" return SuperThing( self.some_default, self.caller.callback_one if hasattr(self.caller,"callback_one") else self.callback_one, self.caller.callback_two if hasattr(self.caller,"callback_two") else self.callback_two ) def callback_one(self): print("The default callback_one handler") def callback_two(self): print("The default callback_two handler") class Other(): def some_method(self): thing = Thing(self) thing.do_it() def callback_one(self): print("This is an overridden callback_one handler") other = Other() other.some_method() 

“Other” is not related to “Thing” or “SuperThing” at all, but it does make sense for it to have the ability to provide its own callback handlers – and I want “Thing” to pass the call over to “Other” if it defines a matching handler. I’m sure this horrible pattern breaks many rules, and I would love to be told off for being an idiot, so although the pattern works I’d appreciate if you could tell me what’s wrong with it!

submitted by /u/BenthicSessile
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *