1 / 17

A Model For The Python Interpreter

A Model For The Python Interpreter. What happens when you…. Type in an expression (anything other than assignment statement…for now)? Answer: the expression is evaluated (and its textual representation is printed if you are in the interactive environment of IDLE)

hansel
Download Presentation

A Model For The Python Interpreter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Model For The Python Interpreter

  2. What happens when you… • Type in an expression (anything other than assignment statement…for now)? • Answer: the expression is evaluated (and its textual representation is printed if you are in the interactive environment of IDLE) • Rule #1: Basic expressions get evaluated to themselves • Rule #2: Compound expressions get evaluated in meaningful ways ( exp1 + exp2 => value of exp1 + value of exp2) • Rule #3: Names get evaluated to the values assigned to them before (more on this later) • Rule #4: Function invocations? Hmmm…

  3. What happens when you… • Type in an assignment statement? [a = 5] • Answer: Several things happen. • Python creates a table before everything runs • The right-hand-side of the equal sign is evaluated (following the rules in the last slide) • If the left-hand-side is a name, look it up in the table. If found, change its value; otherwise create a new entry with the name, and the value from step 2. • (Provisional) If the left-hand-side is a location, e.g.,myName[4], then (a) look up the name (or fail) (b) change the "thing" at that location to the resulted value

  4. Rules For Interpreting Definition StatementHow To Put A Spell Into Spellbook • Select an available slot in the heap (backpack), and associate that slot with your function name in the function table (spellbook) • In that slot, record the name of the argument (reagent), by which you refer to it within the body of function (incantation) • Also record the body of the function (incantation)

  5. Function Application ExpressionSpell Invocation • Examples • f(4) • myFunc(3, 5) • anyFunc() • … • General form: • expr1() or expr1(expr2) or expr1(expr2, expr3) or … • If expr1 evaluates to a Function, it's a function application expression • For now, expr1 will always be a name

  6. Function Application Rules • Evaluate the argument, using the current variable table • Create a new blank variable table on top of the original variable table (enter a dungeon), and the name of the argument (according to the definition) gets the value from step 1. • Interpret the body of the function as if it is a separate program (the content of the original variable table is not available to you) • Upon return (or reaching the end of the function), throw away the new variable table (dungeon map) and go back to the original (leave the dungeon). The function application expression is evaluated to the value of the return expression

  7. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)

  8. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)

  9. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)

  10. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 t = t + y return t y = addOne(x)

  11. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)

  12. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)

  13. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)

  14. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 t = t + y return y y = addOne(x)

  15. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)

  16. Program Quest x = 5 y = 10 x = 2 + 4 x = 6 def addOne(t): y = 1 y = t + y return y y = addOne(x)

  17. To Avoid Insanity • Whenever you use a name within a function definition, make sure it's either one of the argument(s) or has been assigned a value before in the same definition • Try not to modify the argument within the body of the function • Never declare functions within other functions • There are legitimate programs (in fact, almost all) that violate the above rules, but don't do that yet.

More Related