Non-local references in procedures, Python Programming

Assignment Help:

Non-local references in procedures

There  is an important subtlety in the  way  names  are handled in the  environment created by a function call. When  a value  that  is not related in the local environment is referenced, then  it is create up in the chain of base environments. So, as we have look, it is ?ne to have

 

a = 2

def b():

return a

 

When  a name  is assigned inside  a method, a new  relation is created for it in the environment associated with the current call of that method. So, it is ?ne to have

 

a = 2

def b():

a = 3

c = 4

return a + c

 

Both assignments cause  new bindings to be made  in the local variables, and  it is those  bind­ ings  that  are used  to supply values  in the return statement. It will not modify  a in the global environment.

 

But here is a program fragment that gives trouble:

 

a = 3

def b():

a = a + 1

print a

 

It seems  completely understandable, and  you might  expect b() to return 4. But, instead, it produces an error.  What  is going  on??  It each one has to do with  when  Python decides to add  a binding to the local function. When it takes this method de?nition, it sees that  the name  a occurs  on the left-hand-side of an assignment expression, and so, at the very start, it gives a new entry for a in the local variables, but without any number  related to it. Now,  when  it is time to compute the statement

 

a = a + 1

 

Python starts by evaluating the expression on the right hand side: a + 1. When it tries to look up the name  a in the procedure-call environment, it search that a has been included to the environment, but has not yet had a number  given. So it gives an error.

 

In Python, we can write  code to increment a number named in the global variable, by using the global declaration:

a = 3

def b():

global a

a = a + 1

print a

>>> b()

4

>>> b()

5

>>> a

5

 

The statement global a asks that  a new  binding for a not  be made  in the procedure-call environment.  Now,  all instances to a are to the related in the module's environment, and  so this procedure actually modifies a.

 

In Python, we can only make  assignments to names  in the procedure call environment or to the module variable, but not to names  in intermediate variables. So,

 

def outer():

def inner():

a = a + 1

a = 0

inner()

 

In this example, we get a exception, because Python has build a new binding for a in the environment for the invoke to inner.  We could really like inner to be able to see and modify the a that belongs to the scope for outer,  but  there  is no way  to manage this.   Some  other  programming languages, such as procedure,  offer more  flexible grained control  over how  the scoping of variables is handled.

 


Related Discussions:- Non-local references in procedures

#cmdprobs, #Why can''t I cd my Desktop? A minute ago I would open up cmd an...

#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

Lists, Python has  a built-in list data  structure that  is easy  to use  a...

Python has  a built-in list data  structure that  is easy  to use  and  incredibly convenient.  So, for that point, you can say >>> y = [1, 2, 3] >>> y[0] 1 >>> y[2]

Program to solve word search puzzles, This assignment involves writing a pr...

This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. N

Basics of python-introduction, Basics of python-introduction Python is...

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

Indentation and line breaks, Every programming language has to have  some m...

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

Python Program Help, Write a Python program to accomplish the following. U...

Write a Python program to accomplish the following. Use modular design. Include at least 3 functions: one that returns zero values, one that returns one value, and one that retu

Lcr circuit, program on damped lcr cicuit

program on damped lcr cicuit

Packages and Libraries, For this assignment, you need to create a program t...

For this assignment, you need to create a program that allows the user to do basic trigonometry functions. First, ask the user if he or she would like to do sine, cosine, or tangen

Lab programming, protocol rdt2.2 considers there is a bit errors between se...

protocol rdt2.2 considers there is a bit errors between sender to receiver and also from receiver to sender. So now we have to consider checking bit errors introduced in reply from

Random Geometric Graphs, Displaying random geometric graphs in an uniform s...

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

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd