-1

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.

  • `sup.method(obj).`, IMHO – Thomas Weller Jun 29 '20 at 09:19
  • I don't think `((sup) sub).method()` would skip the child implementation in Java either, that's not how casting works as far as I understand it. Could you give some *context*, what are you actually trying to do here? Why do you want to skip the child implementation? Are you looking for e.g. `super`? – jonrsharpe Jun 29 '20 at 09:21
  • [Call a parent class's method from child class](https://stackoverflow.com/questions/805066/call-a-parent-classs-method-from-child-class/805081#805081) – DarrylG Jun 29 '20 at 09:26
  • @jonrsharpe: in C# it would call the base method only (if not defined as virtual) – Thomas Weller Jun 29 '20 at 09:33
  • @DarrylG: I don't think OP wants to call it from the child class but from code outside of the child class – Thomas Weller Jun 29 '20 at 09:34
  • Thank you all. I apparently had not completely comprehended the way self works. I see it better now, thanks Thomas Weller. jonrsharpe i could make an use case of why i use this but currently as i stated it is just for learning purposes. Thanks DarrylG i didn't found that because i was apparently too focused in achieving the cast-like thing. Looked at it and it helps. Thank you all. – Ilab Sentuser Jun 29 '20 at 09:36
  • @jonrsharpe in java applies the same as c# in this as noted by Thomas Weller. It would call the base method since you are explicitly casting it(unless virtual like previously said for c#) – Ilab Sentuser Jun 29 '20 at 09:45
  • @IlabSentuser--[Trying to cast one object type into another in Python](https://stackoverflow.com/questions/31550983/trying-to-cast-one-object-type-into-another-in-python) may help clarify for your learning purpose. It explains with Python use of "duck-typing" it doesn't need to cast one type into another. – DarrylG Jun 29 '20 at 09:48
  • Thank you @DarrylG i found that while searching but even though i understood it i couldn't figure out how to adapt it to my case. Since what i actually wanted(didn't noticed it until the answers and comments came in) was not actually cast the object but to access members of its base class. I will edit the title to reflect this. Thank you. – Ilab Sentuser Jun 29 '20 at 09:58
  • @IlabSentuser--in that case you probably realize there are two methods [Python: Call Parent class method](https://www.geeksforgeeks.org/python-call-parent-class-method/). – DarrylG Jun 29 '20 at 10:05
  • @DarrylG thanks for the link, i did knew that, but my case was calling them from outside the class, not from subclasses. But i found slme interesting things while surfing through that link. Thank you very much. – Ilab Sentuser Jun 29 '20 at 11:11

1 Answers1

0

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

Alexander Kosik
  • 669
  • 3
  • 10
  • Thank you @Alexander Kosik. I apparently have not completely understood the way self works. I get it now. I have marked this as answer. Also thank you for pointing to PEP8 although i haven't delved very much in python yet, but i should had written it in uppercase anyway, thanks for pointing that out too. – Ilab Sentuser Jun 29 '20 at 09:41