Scope in nested functions - scope

g = 3; # A global
def sub1():
a = 5; # Creates a local
b = 7; # Creates another local
. . . <------------------------------ 1
def sub2():
global g; # Global g is now assignable here
c = 9; # Creates a new local
. . . <------------------------------ 2
def sub3():
nonlocal c: # Makes nonlocal c visible here
g = 11; # Creates a new local
. . . <------------------------------ 3
The book I am reading mentions the fact that a and b of sub1() are not visible in 3, although sub3() is more deeply nested than sub1() because a and b are dynamic stack variables.
sub3() can be executed without the execution of sub1() and hence a and b are not necessarily allocated memory. But it also says that c of sub2() is visible in 3. Why is the same logic not applied here?

Related

Access to the local environment of a function from another function called by the previous one

Let's take a look at this piece of code:
def a(): # N = 0
string = "pizza"
# stuff
res_b = b(string)
def b(string): # N = 1
# stuff
res_c = c(string)
return res_c
def c(string): # N = 2
# stuff
return thing
I have a long file which has basically the same shape than that. I would like to remove the parameter str from the definitions and to make b and c able to read it directly (I mean not using an external dictionary) from the N-1 function. So I wonder if a function could read the local environment of the one which called it.
Does anything look like what I am looking for ?

A small question about the change() function,how to understand the process about it?

I am learning the basic usage of python,and I'm confusing about how the variable runs in a practice question. Here are the code below:
x = 1
def change(a):
a = x + 1
print(x)
change(x)
x = 1
def change(a)
x = x + 1
print(x)
change(x)
This is how I think the process:
in the first code:change(x) means: x = x + 1 - print (x) - output:2
but in fact the result is 1.So the real process is: x(symbol in the function) = x(global variable) + 1, print(x), this x is the global variable.
is that right?
in the second code,I think still the output should be 2,but it shows me that UnboundLocalError: local variable 'x' referenced before assignment
so in the python,we can't use function to change the global variable?
Inside a function, you can read global variables but you cannot modify them without explicitly declaring them as global like this:
x = 1
def change(a):
global x # <- this is required since you're assigning a new value to x
x = x + 1
print(x)
change(x)
In the first case, with a = x + 1, the global declaration is not required since you're only reading the value of x, not modifying it. Also, the output is 1 in the first case, since you're printing x not a.

Assigning multiple expressions for Symbolic variable in theano

a = T.iscalar()
b = T.iscalar()
c = a
d = c+b
f = theano.function(
inputs=[a,b],
outputs=d
)
print(f(2,3))
This piece of code is executing properly and is printing 5. But if I change the code as below,
a = T.iscalar()
b = T.iscalar()
e = T.iscalar()
c = a
d = c+b
c = e
f = theano.function(
inputs=[e,b],
outputs=d
)
print(f(2,3))
I'm getting the following error:
theano.compile.function_module.UnusedInputError: theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index 0 is not part of the computational graph needed to compute the outputs:
It seems that c is depending only on a and not on e.
My question is, Does theano allows only one expression for the assignment of a symbolic variable?

How to import variables from another script python 3

How can I import a variable from another script using python 3?
Example:
I have two scripts that we shall call script_1.py and script_2.py.
script_1.py:
class Calculate():
def addition():
a = 5
b = 2
c = a + b
Q: How can I use this second script (script_2.py) to print the variable c from the script_1.py?
You cannot do this since c is not a global variable and doesn't even seem to exist outside of addition. Even if it does exist outside of your class, then the addition method (which should either be def addition(self) or be declared with #staticmethod decorator by the way) won't change it since it's not declared as a global variable in it.
Script 1
class Calculate():
#staticmethod
def addition():
a = 5
b = 2
c = a + b
return c
Script 2
from script_1 import Calculate
print(Calculate().addition())
Will output the value of c (eg 7).
If you need a global variable (you almost certainly don't) :
Script 1
c = None
class Calculate():
#staticmethod
def addition():
global c
a = 5
b = 2
c = a + b
Script 2
from script_1 import c, Calculate
Calculate().addition()
print(c)
You really shouldn't do this. Global variables can lead to serious problems. Global constants however are usually ok.

Evaluation of variable through Pass By Name

I have a doubt regarding Pass By Name
Procedure test ( int c, int d)
{
int k = 10;
c = 5;
d = d + 2 ;
k = c + d;
print (k);
}
main()
{
k = 1;
test(k,k);
print (k);
}
I did refer to one of the earlier question on what is pass by name and how does it work
and the link given in it :
Pass by name parameter passing
The Question i have is : will the above code print : ( 14 , 1 ) or (14, 14)
Basically doubt is whether the value of k in procedure be reflected in main procedure or not.
I'm preparing for an exam. This's a code snippet given in one of the question bank.
Pass by name, when you are passing a variable and not a more complex expression, behaves the same as pass by reference. Thus, your code prints 14 and 7.
Note that the local variable k in your procedure test is not the same variable as the global variable k. In test, the assignments c = 5 and d = d + 2 both assign to the global k, as it was passed by name to test through both c and d. Thus, after these assignments, the global k has the value 7. The assignment k = c + d; affects the local variable k (as that is the k in scope at that time), not the global variable k (which is shadowed by the local variable), and thus the global k retains the value 7 after the assignment.

Resources