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
You should use for whenever you can, because  it creates  the structure of your  loops clear. Sometimes, however, you require to do an operation various times, but you don't want t

Displaying random geometric graphs in an uniform square, unit disk, dense rim unit disk, and uniform sphere

Write a program that asks the user to enter a number of seconds. The responses of the program will vary depending on the length of seconds: • If the number of seconds is under 60

I have python game project which is due by next Tuesday. Do you think that I can get it on time if I order today?

Finally! After years of adding unimportant sports, the IOC has finally added Shoe Tying to the Olympics. And you’ve been selected to write the software to handle the judging of t


What are squared matrices?

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

Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the tota

Define a function that draws a pentagon of a size you provide when you call the function. Include a ''''''docstring'''''' as the first line of the function''s body — it tells the p