Về cách hoạt động của hàm super() trong đoạn code Python này

Mình đang học LPTHW và chưa hiểu cách hàm super() hoạt động thế nào. Mọi người giúp mình nhé.

Code:

class Parent(object):

    def override(self):
        print "PARENT override()"

    def implicit(self):
        print "PARENT implicit()"

    def altered(self):
        print "PARENT altered()"

class Child(Parent):

    def override(self):
        print "CHILD override()"

    def altered(self):
        print "CHILD, BEFORE PARENT altered()"
        super(Child, self).altered()
        print "CHILD, AFTER PARENT altered()"

dad = Parent()
son = Child()

dad.implicit()
son.implicit()

dad.override()
son.override()

dad.altered()
son.altered()

super() là gọi class mẹ của nó ( Parent).
trong này là gọi tới hàm altered của vlass Parent

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?