Namespace, scope, compile time activities, runtime activities When do the small integer values get...

102
Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace get there before compilation started? Assume there are no syntax errors. In the follow code, assume the user types 2 when prompted. Notice that we are using an int value for the price of coffee in this version.

Transcript of Namespace, scope, compile time activities, runtime activities When do the small integer values get...

Page 1: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

Namespace, scope, compile time activities, runtime activities

When do the small integer values get stored in RAM?

How did the names in the builtin namespace get there before compilation started?

Assume there are no syntax errors.In the follow code, assume the user types 2 when prompted.

Notice that we are using an int value for the price of coffee in this version.

Page 2: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

Show what happens at each stepduring compilation

and runtime……and before

Page 3: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

RAM

start IDLE

Page 4: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

RAM

5

4

3

2

1

start IDLE – small integers and single character strings are loaded into RAM

Page 5: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

__builtin__ module is imported

RAM

5

4

3

2

1

namespaces

builtin lenprintroundglobal

Page 6: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

compilation proceeds from top to bottom in source file – line 1.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

Page 7: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 1. global constant PRICE_PER_CUP added to global namespace and bound to 1

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

Page 8: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 2.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

Page 9: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 2. the function name added to global namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

Page 10: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 3.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

Page 11: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 3. docstring added to help system

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 12: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 4.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 13: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 4. RHS looks fine – no searching is done

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 14: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 4. no input, no function calls

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

NO INPUT

''' returns user's coffee order quantity '''

Page 15: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 4. cups is added to getNumCoffeeCups’ local namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

NO INPUT

''' returns user's coffee order quantity '''

Page 16: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 4. no assignment is done – assignment is a runtime activity, no arrow appears

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

NO INPUT

''' returns user's coffee order quantity '''

Page 17: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 5.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 18: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 5. looks like a return statement – no searching is done.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 19: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 5. no value is returned – this is compile time. returning a value is a run time activity

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 20: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

getNumCoffeeCups’ compiled body is placed into RAM, ready to be called – no run

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 21: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 6.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 22: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 6. the function name is added to global

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

Page 23: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 6. parameter name is added to calcOwed’s local namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity '''

Page 24: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

notice that there are two “cups” names in two different namespaces – different scopes

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity '''

Page 25: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

getNumCoffeeCups’s creator chose a good name. calcOwed’s creator chose a good name.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity '''

Page 26: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

calcOwed’s creator doesn’t even know that any other functions exist

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

Page 27: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 7. – docstring added to help system

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

Page 28: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 8. looks like an arithmetic expression and a return statement – no searching is done

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

Page 29: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

calcOwed’s compiled body is placed into RAM, ready to be called – no run happens

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

Page 30: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 9.

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

Page 31: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 9. – function name is added to global namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

Page 32: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 9. – parameter names are added to showBill’s local namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

Page 33: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 10. – docstring is added to help system

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 34: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

lines 11. and 12. – print() looks like a function call – no searching is done

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 35: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

lines 11. and 12. – those str()’s look like a function calls no searching is done

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 36: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

lines 11. and 12. – str expression compiles – everything is fine

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 37: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

showBill’s compiled body is placed into RAM, ready to be called – no execution, no run

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 38: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

notice that there are three “cups” names in three different namespaces – different scopes

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 39: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

again, no conflict because the three ‘cups’s are in different scopes – different namespaces

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 40: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line13. the function name: main is added to the global namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 41: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

there is no docstring written under main’s def line – the program level docstring is at the top

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 42: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 14. no function call happens – this is compile time, function calls happen at run time

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 43: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 14. getNumCoffeeCups() looks like a function call so compiler is happy

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 44: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 14. the assignment statement does not happen – it will at run time, not now

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 45: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 14. cups is added to the main’s local namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 46: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 15. again no function call happens – it looks like a function call - fine

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 47: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 15. no search is made to see if cups already exists – that will be done at run time

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 48: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 15. the assignment statement is not executed

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 49: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 15. owed is added to main’s local namespace – stil no name conflicts!

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 50: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 16. looks like a function call – no searching happens – no call happens

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 51: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

main’s compiled body is placed into RAM, ready to be called – no execution, no run

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 52: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

compile time has ended – now we enter exectution or run time at the call of main()

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

cups

cupsowed

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 53: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

compile time has ended – now we enter exectution or run time at the call of main()

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 54: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

run time order is the order of function calls and returns and starts with the call to main()

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 55: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the name main is searched for – LEGB and found –NameError does not occur

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 56: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

if there is no communication through parameters, execution continues in the body of main

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 57: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the RHS of the assignment is evaluated – it’s a function call, so…

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 58: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

…the name getNumCoffeeCups is searched for LEG and found in the global namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 59: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

since there is no communication via parameters, the first line in getNumCoffeeCups runs

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 60: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

raw_input is searched for – LEGB finally found in the builtin namespace – no NameError

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 61: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

raw_input is passed the prompt string and it shows it to the user

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 62: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the user hits the 2 button, hits the Enter button, the newline is trashed, the str ‘2’ is returned

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 63: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the name int is searched for – LEGB and found in the builtins

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 64: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the returned ‘2’ is passed to int(_) which returns the int 2 which is now the value of the RHS

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 65: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

now that the RHS’s value is fully known, the run time system looks to see if the 2 is in RAM

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 66: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the value of the int(raw_input(“..”)) function call expression becomes the returned 2

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 67: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the value of the int(raw_input(“..”)) function call expression becomes the returned 2

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 68: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the LHS, cups, is searched for: L – found in the local namespace of getNumCoffeeCups

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 69: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

cups is made to point at where the 2 is in RAM – cups is bound to the RAM containing the 2

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 70: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

notice that there is no confusion about which ‘cups’ is assigned to – it’s scope is local

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 71: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

return cups – cups is again searched for and found in the locals of getNumCoffeeCups

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 72: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

return returns to it’s caller a reference to where the 2 is in RAM

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 73: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the value of the function call expression becomes the value returned, the run continues there

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 74: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

…and all local references back at the call go away – cups stays in the local namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 75: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

…and all local references back at the call go away – cups stays in the local namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 76: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

getNumCoffeeCup’s local cups is no longer in scope when its lifetime ended after the return

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 77: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

main’s local cups is searched for in main’s local and found and the assignment occurs

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 78: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

main’s local cups is searched for in main’s local and found and the assignment occurs

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''‘ 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 79: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

line 15. – function call on RHS – calcOwed is found in the globals – no NameError

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 80: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

cups is searched for and found in main’s locals…

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 81: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

setting up the call to calcOwed - its parameter name ‘cups’ searched for and found locally

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 82: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

calcOwed’s local parameter name ‘cups’ is assigned main’s local variable ‘cups’

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 83: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

this is pass by asasignment

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 84: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

now that the communication via parameters has been set up, the body is executed

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 85: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the expression to be returned is evaluated, cups is found in calcOwed’s locals

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 86: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

PRICE_PER_CUP is searched for – LEGB, found in the global namespace

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 87: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

cups * PRICE_PER_CUP is evaluated and that value is searched for in RAM and found

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 88: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the expression becomes that value

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 89: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

back at the call, the function call becomes the returned value

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 90: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

and all local references in calcOwed go away – they are no longer bound to values in RAM

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 91: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

and all local references in calcOwed go away – they are no longer bound to values in RAM

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 92: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

execution continues on the line with the call

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 93: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

owed is searched for and found in main’s local namespace…

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 94: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

…and the assignment is made

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 95: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

we’ll speed things up a bit now

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 96: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

find showBill in the globals, cups and owed in the main’s locals

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 97: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

find showBill’s parameters

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 98: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

pass by assignment

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 99: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

find print and str in builtins, look up owed’s and cups’ values

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 100: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

the string is built, looked for in RAM, not found, stored in RAM somewhere, passed, printed

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2You owe $2for your 2 cup(s)

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 101: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

execution returns to the caller: main(), local references in showBill go away, names stay

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2You owe $2for your 2 cup(s)

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

Page 102: Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

main() call is over, local references in main go away, execution ends – WHEW!

code

1. PRICE_PER_CUP = 1 # in US$

2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups

6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP

9. def showBill( cups, owed ):10. ''' presents bill nicely '''11. print( "You owe $" + str( owed ) + "\n" + \12. "for your " + str( cups ) + " cups(s)" )

13. def main():14. cups = getNumCoffeeCups()15. owed = calcOwed( cups )16. showBill( cups, owed )

17. main()

namespaces

builtin lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2You owe $2for your 2 cup(s)

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''