Home > OS >  Polymorphism and calls from a module
Polymorphism and calls from a module

Time:04-01

I'm having trouble with the use of polymorphism in object oriented programming in python. Here I define a specialisation of the list class that overrides the append and setitem methods

class MyList(list):
    def __setitem__(self, key,value):
        super(MyList,self).__setitem__(key,value)
        print("bubu")
    def append(self,value):
        super(MyList,self).append(value)
        print("bibi")

Now if I test this class with

myobj = MyList()
myobj.append(1) 
myobj[0]=3

I get "bibi", and then "bubu", as expected. But if I use a MyList object with an external module

import heapq
myobj = MyList()
heapq.heappush(myobj,1) 

The function heappush from heapq is supposed to call myobj.append (I checked the sources https://github.com/python/cpython/blob/3.10/Lib/heapq.py). But I get no "bibi" this time ! Isn't the "append" called supposed to be that of the instance passed in arguments ?

Where are my bibis ?? :)

CodePudding user response:

If you run Python without any arguments and enter the following, I am fairly certain you will get no import error:

>>>
>>>
>>> from _heapq import *
>>>

I mention this because if you just look a little more deeply into the source code for heapq, you will see at the bottom:

# If available, use C implementation
try:
    from _heapq import *
except ImportError:
    pass
... # etc.

And since we had no reason to believe your MyList implementation of method append was not working correctly, that alone should have suggested that the heapq.heappush method was not calling the append method on the list. And now we no why.

  • Related