Note: I found this to be a duplicate and an additional answer in another thread so i am linking to it in case you want to see both.
Calling base class method in Python
Note: this question title was changed due to not correctly illustrating what was desired. Previous title was: Python equivalent way of casting subclasses to superclasses
I am trying to figure out how to simulate (since as far as I know Python does not support it) class object casting. For example let's suppose I have 2 classes, sup and sub:
class sup()
...
def method(self):
...
class sub(sup):
...
def method(self):
...
I know that since method exists in both superclass and subclass it is going to be overriden. But I want to achieve something like this:
sub obj()
((sup) sub).method()
in order to call the superclass method and not the subclass one. In Java I would have done it like that, but i have read and python does not have this functionality (correct me if I'm wrong, it is what I have investigated so far) so I am looking for an equivalent way of doing this.
You can pass the current object to the Base Class:
o = sub()
sup.method(o) # this will call method of the base class
Just another hint: in python it is is common to write types in uppercase, so in your example Sub
and Sup
, see PEP8