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
Variables We cannot  go very far without variables. A variable is a value related to a name that we can bind  to have a particular value  and  then  later use in an expression.

Task (Anagrams) Write a function that checks whether two words are anagrams. Two words are anagrams if they contain the same letters. Fore example, silent and listen are anagrams.

Normalize a vector Let's imagine we want  to normalize a vector  of three  variables;  that  is to compute a new  vector  of three values,  such that its size  is 1. Here is o

What if you had a list of integer values, and you need to add  them  up and give the sum?  Here are a number of different types of doing  it. First,  here  is a type in a style

I have a C++ code (10-15 line) which i need to re-write in python with the help of ctypes library. Is it possible i can get some help with it? Thanks & regards Tanmoy

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

#question.Program 10 Assignment (Banking Objects – Savings/Checking Accounts) Create a program named 10.py that performs the following: Create a class named ChkAcct, and a class n

Structured data We will often  want  to work  with  large  groups of data.   Rather  than  providing  each  number its own  value,  we need  to manage the data  into natural s

how to use re.sub to seperate distint words and phrases when translating text from English to Spanish or vis versa in python programming

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