Python Forum

Full Version: how can I changing a base class method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
I don't believe you can. And if you can, it's would be a bad idea.
Thanks for this awesome answer !
I agree with you , it is a bad idea, in fact programming in general is a bad idea !