Can an method which is overridden still call the parent method if needed?

Question

If a class overrides a method from the parent class, is it still possible to call the parent class method from the inheriting class if that is useful for the class to do?

Answer

Yes, the overriding method in the class can still call the method in the parent class if doing so will provide some benefit. Depending on whether you are using Python 2 or Python 3, the syntax for calling the parent class can be different. The following code example shows three different methods to make the call from the inheriting class B method to the parent class A method.

class A(object):
    def do_something(self, parm1):
        print("A - do_something - was passed " + str(parm1))

class B(A):
    def do_something(self, parm1):
        # Works for both Python 2 and 3
        A.do_something(self, parm1 + 1)

        # Python 2 - this only works if A inherits from object
        # super(B, self).do_something(parm1 + 1)

        # Python 3 -
        # super().do_something(parm1 + 1)
        print("B - do_something - was passed " + str(parm1))


obj = B()

obj.do_something(100)

#OUTPUTS:
# A - do_something - was passed 101
# B - do_something - was passed 100
9 Likes

Should we always include overridden method inside remixed parent method we want access it ?

From a practical standpoint, is someone able to provide a use case? While I understand the logic behind it, I’m struggling to think of a situation where this may be useful.

Thanks!

1 Like

Not something I use very often. The only practical example that comes to mind is something like this:

class PostIndex:
   def authorize(self, user):
     return user.has_role('some_role')

class PostEdit(PostIndex):
  def authorize(self, user, post):
     return PostIndex.authorize(user) && post.belongsToUser(user)

Only users with a specific role should be able to see posts, and for edit only the user who created the post should be able to edit. Now I don’t have any duplicate logic

Not sure this is the greatest example, if the users permission now changes they might no longer be able to edit there own post.

1 Like

Thanks @stetim94 ! It makes more sense then it did before. If i’m going to be completely honest, I’m still a bit iffy, but I’m sure it’ll come with more practice. Appreciate your help!

Well, as long as you understand the concept, with a bit more practice you will see when and where its useful (and when not )

1 Like

What is ‘&&’ supposed to be doing here? I tried searching the web for this but it doesn’t appear to be valid Python syntax…

1 Like

In many programming languages, && is the and operator. In python its just and. So that both conditions need to be true. I confused syntax sometimes, happens when you know so many programming languages and you just write a quick draft of something that doesn’t actual have to be functional

1 Like

Is someone able to clarify the difference between the Python 2 and the Python 3 code? I did not really understand the comments in class B.

python3 introduced simpler syntax of using super(), as explained here:

inheritance - Python extending with - using super() Python 3 vs Python 2 - Stack Overflow

1 Like

Thanks, sometimes some of these FAQs get a little bit confusing because the answers start discussing content featured in future exercises or methods without fully discussing or explaining those concepts. This is particularly confusing when an FAQ is featured in one exercise, but the content discussed in the FAQ assumes knowledge learned in a future exercise.

2 Likes