Python Forum
how can I changing a base class method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can I changing a base class method
#1
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)
Reply
#2
I don't believe you can. And if you can, it's would be a bad idea.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for this awesome answer !
I agree with you , it is a bad idea, in fact programming in general is a bad idea !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Using one child class method in another child class garynewport 5 1,484 Jan-11-2023, 06:07 PM
Last Post: garynewport
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,380 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,680 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Calling a base class variable from an inherited class CompleteNewb 3 1,591 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  anonymous method in a class Skaperen 8 3,477 May-23-2021, 11:17 PM
Last Post: Skaperen
  Importing issues with base class for inheritance riccardoob 5 4,589 May-19-2021, 05:18 PM
Last Post: snippsat
  How to apply a class method to an entire dataframe column tirtha9 1 5,052 Jan-03-2021, 04:44 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020