paul@657 | 1 | Inlining can be considered similar to grammar productions. The path of
|
paul@657 | 2 | execution in Python is somewhat indirect due to the need to branch
|
paul@657 | 3 | continuously in order to perform work. Consider the following function calls:
|
paul@657 | 4 |
|
paul@657 | 5 | x(n)
|
paul@657 | 6 | y(n)
|
paul@657 | 7 | z(n)
|
paul@657 | 8 |
|
paul@657 | 9 | Where x, y and z are identifiable in advance and are stable, one can consider
|
paul@657 | 10 | the contents of these functions together. We can write the executed code using
|
paul@657 | 11 | the following abstract representation:
|
paul@657 | 12 |
|
paul@657 | 13 | XYZ
|
paul@657 | 14 |
|
paul@657 | 15 | However, it is rather likely that one instead sees things like this:
|
paul@657 | 16 |
|
paul@657 | 17 | a.x(n)
|
paul@657 | 18 | b.y(n)
|
paul@657 | 19 | c.z(n)
|
paul@657 | 20 |
|
paul@657 | 21 | Here, the functions called depend on what a, b and c are. This might be
|
paul@657 | 22 | written as follows using the abstract representation, borrowing from regular
|
paul@657 | 23 | expression notation:
|
paul@657 | 24 |
|
paul@657 | 25 | (X|P)(Y|Q)(Z|R)
|
paul@657 | 26 |
|
paul@657 | 27 | Here, P, Q and R are alternatives for X, Y and Z respectively. Within each of
|
paul@657 | 28 | these functions, other code is executed, and we might want to expand the
|
paul@657 | 29 | top-level "productions" to show the actual operations performed inside those
|
paul@657 | 30 | functions. Consider Y and Q being defined as follows:
|
paul@657 | 31 |
|
paul@657 | 32 | def Y(n):
|
paul@657 | 33 | return n
|
paul@657 | 34 |
|
paul@657 | 35 | def Q(n):
|
paul@657 | 36 | return n and Q(f(n))
|
paul@657 | 37 |
|
paul@657 | 38 | This might change the abstract representation as follows:
|
paul@657 | 39 |
|
paul@657 | 40 | (X|P)(Y|F*)(Z|R)
|
paul@657 | 41 |
|
paul@657 | 42 | Here, Q evaluates n and either returns n immediately or calls itself with the
|
paul@657 | 43 | result of f (which we will consider as identical to F). Where Q does not have
|
paul@657 | 44 | a value of n that causes it to return immediately, F is invoked, and so F is
|
paul@657 | 45 | invoked repeatedly until a final invocation of Q that returns immediately.
|