Skip to content

Class inheritance demonstration

dours edited this page Jul 8, 2022 · 2 revisions

Consider this python code

  def myC():
      class c:
          value = 11
          def getValue(self): return self.value
      class d(c): pass
      o = d()
      return o.getValue() == 11

Each class is translated to a function, which returns a new object of the class when called. That is, a constructor for the class. This is done because it is not easy to statically distinguish a function call from a class creation operator in python: they look very similar. Now consider the result of generation of the class d to EO.

[]
  newUID.apply 0 > x__id__
  [] > apply
    [stackUp] > @
      cage result > pResult
      cage 0 > tmp
      [] > result
        goto (((xc).apply).@) > base
        xd > x__class__
        seq > initFields
          base.result.x__class__.x__id__
        base.result > @
      seq (result.initFields) (pResult.write result) (stackUp.forward (return pResult)) > @

First, there is a "standard" header of a function. A function is an object with method apply, which accepts parameters of the function (no parameters in this case). apply contains an inner function, which is used to be called with goto.

[]
  newUID.apply 0 > x__id__
  [] > apply
    [stackUp] > @

Then, the actual construction code. It creates a class object called result, initializes and returns it.

      cage result > pResult
      cage 0 > tmp
      [] > result

Here a constructor of an object of class c is called.

        goto (((xc).apply).@) > base
        xd > x__class__
        seq > initFields
          base.result.x__class__.x__id__

Here we actually delegate to the base object. We need to take field result of base because a constructor uses the wrapper object called return to return its result.

        base.result > @
      seq (result.initFields) (pResult.write result) (stackUp.forward (return pResult)) > @

Clone this wiki locally