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
For and While   If we want  to do some operation or set of operations various  times, we can handle the process  in several  different types.  The most straightforward types

Synthetic models One  goal  of various  people in a variety of sub-disciplines of  electrical  engineering and  computer science is automatic synthesis of machine from  formal

from urllib2 import urlopen # Open http://placekitten.com/ for reading on line 4! response = kittens.read() body = response[559:1000] # Add your ''print'' statement here!

Primitives, Composition, Abstraction, and Patterns   We will  start  by thinking about  how  the  PCAP  framework applies to computer programs, in general. We may do that by

draw the variation of pressure with volume of a real gas at temperatures lower than its critical temperature.also draw its surface of discontinuity

python program of motion of a particle in viscus medium

Write a program that requests a password after the author/program information is displayed. Make the password "hello". The program should then ask the user for their name: if the n

Python Programs You must submit the source code and samples of output for each program.Please do not provide python file (eg. Filename.py). Copy all source code to one word fi

List mutation and shared structure Lists are mutable data  structures, which defines  that  we can actually modifies  the values  stored in their components. We do this by usi

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