lpython
lpython copied to clipboard
Implement inheritance
Example :
from lpython import i32
class Base:
def __init__(self:"Base"):
self.x_A : i32 = 10
class Derived(Base):
def __init__(self:"Derived") :
super().__init__()
self.y_B : i32 = 6
def get_x_A(self:"Derived"):
print(self.x_A)
def main():
d : Derived = Derived()
print(d.x_A)
print(d.y_B)
d.get_x_A()
main()
Can we create a var super and add it to the symtab of the derived class?
super : Base = Base()
So we can search the symtab of the derived and if the member / member fn is not found , we can move to the symtab of the super. Reference - https://docs.python.org/3/library/functions.html#super
@certik, What do you think the best approach here?
super() in Python is a function. Is the idea to add this function as a method to a class? In Python it's not a method, but a global intrinsic function. Or is the idea to add a super variable? That also seems would differ from CPython.
I would handle super() in the frontend only, and in ASR we need some mechanism to call methods of the parent class. I think you can just call any method directly from the parent symtab, cannot you?
So it seems it's not needed to put super into the symbol table in any form?
Ok got it 👍🏻
Should the members and member functions of the base class be added to the derived class' symtab, or should they be added as an external symbol? @certik @Thirumalai-Shaktivel