Non-local references, Python Programming

Non-local references

So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were evaluating the expression). But take this code:

 

def biz(a):

return a + b

 

>>> b = 6

 

2176_Non-local references.png

 

We need to elaborate, slightly,  how it is that a variable v is computed in an environment E:

 

  • We take if there is a binding for v in E; if so, we stop and return it.

 

  • If not, we compute v in the parent environment of E. If E has no base, we create an error.

 

It is important to check that this method  will continue up an arbitrarily long chain of environ­ ments  and their parents until either a binding for v is found or there are no more environments to examine.

So, in our case, we will compute the expression a + b in environment E2. We start by calculating a and searching value 2 in E2. Then, we compute b and cannot search it in E2...but we don't panic! We take the parent pointer to E1 and  try again.  We search a binding for b in E1 and  get the value  6. So, the value  of a + b in E2 is 8.

 

Posted Date: 8/11/2012 2:43:21 AM | Location : United States







Related Discussions:- Non-local references, Assignment Help, Ask Question on Non-local references, Get Answer, Expert's Help, Non-local references Discussions

Write discussion on Non-local references
Your posts are moderated
Related Questions
#ques Write the pseudocode (use a word processor please) and the Python 3.0 program for the following problem. A monkey is being fed some food. Read in the starting weight in lbs:o

Data array A has data series from 1,000,000 to 1 with step size 1, which is in perfect decreasing order. Data array B has data series from 1 to 1,000,000, which is in random order.

#Why can''t I cd my Desktop? A minute ago I would open up cmd and it starts in C:\Users\myname but now it''s starting in C:\WINDOWS\system> and I don''t where I am or how to get ou

There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many

Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:   if (s == 1){ s = s

Modules As you start to write larger programs, you will want  to save the function de?nitions in multiple ?les, collected together according to what  they  do.  So, for exampl

Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

how do you make an if and else statment work in pytho?

Your assignment for the semester will involve the development of a system for drawing trees using the Python programming language and the turtle graphics module (turtle.py). Comple