how can I changing a base class method - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: how can I changing a base class method (/thread-22374.html) |
how can I changing a base class method - voidptr - Nov-10-2019 Is there some ways to change methods from a base class ? here an example, I want to change the behavior of extendleft form deque. yes I can create a subclass, but I need to rewrite all code that use deque to Xdeque instead. Is there a way to change that method instead in the base class ? from collections import deque class Xdeque(deque): def extendleft(self,items): return super().extendleft(reversed(items)) d = Xdeque() d.extend((4,5,6,7)) d.extendleft((1,2,3)) print(d) RE: how can I changing a base class method - ichabod801 - Nov-10-2019 I don't believe you can. And if you can, it's would be a bad idea. RE: how can I changing a base class method - voidptr - Nov-10-2019 Thanks for this awesome answer ! I agree with you , it is a bad idea, in fact programming in general is a bad idea ! |