Variable between two different definitions - python

I want to call var d from b(). but I get this error. I have heard that you can have global variables which I tried but with no success.
Error:
Traceback (most recent call last):
File "C:/Users/user2/Desktop/def.py", line 9, in <module>
a()
File "C:/Users/user2/Desktop/def.py", line 3, in a
if d == 0:
NameError: name 'd' is not defined
Code:
def a():
if d == 0:
print(correct)
else:
print (not correct)
def b():
d = 0
a()

You can define the variable outside the function and it should work. Although it is better to pass as argument.
d=0
correct="It is correct"
notcorrect="It is not correct"
def a():
if d == 0:
print(correct)
else:
print(notcorrect)
a()

You can use variables of a "parent" scope, even if it's better to pass them to the method. Your function b() is never called in your example. And definitions in a function are just defined for this functions or functions called from there.
I would recommend you to read about scopes:
https://pythonspot.com/scope/ (there are tons of other tutorials out there, just use your search engine ;))
what you could do:
d = 0
a() # correct
what you could do as well:
def b():
d = 0
a()
b() # correct
but what you SHOULD do is probably something like:
def b():
d = 0
return d
def a(d):
...
a(b()) # correct
global variables exist in python, but especially for beginners it often seems to be an easy solution, but as soon as your code grows this can become very complex if not used carefully.. understanding scopes of variables and how to pass them into other functions is the way to go.

The code will definitely show an error because the variables created within the function are for that function only called local variables, and the variable created outside the function are called global variables.
hence,
you simply have to create the variable outside the function globally then it will not shows error.
def a(d):
if d == 0:
print("correct")
else:
print ("not correct")
#################
a(0)
or else you can use inbuilt function global() if you want to use the variable inside the different functions.
and make sure you have the functions as well.

Related

unable to use attribute from a python function in another python script

I want to use one of the attributes returned by a function in a python script (x) into a python script (y)
The communication between both scripts works well; I can get functions and attributes, but doesn't allow me to attributes returned by a function.
Here is how I worked:
x.py
def func():
b = 10
a = 15
return [a,b]
c = 20
y.py
from x import func
import x
print (x.c)
print (func.b)
I get the "c" value and the following error AttributeError: 'function' object has no attribute 'b'
I have tried also to print x.b, and I've got AttributeError: module 'WorkingLSTM' has no attribute 'b'
Thanks in advance
The way to call func is by using func(), which would give you [a,b].
example:
funcResult = func()
a = funcResult[0]
b = funcResult[1]
funcResults is the return value from func(), that is the list [a,b].
That's not allowed, you have to call the function to get the value from the functions returned list.
a, b = func()
print(b)
# or just...
print(func()[1])
PS: It's "not allowed" because it doesn't make sense in any way; when there is no function call, there is not variable b at all. You might take a look at classes, they can hold static variables.
you cannot access local variables of a function.
these variables exist only during the the time where func is executed and are destroyed afterwards.
You can of course call the function and look at the result, but the result is just a list with two values
rslt = func()
print("A = ", rslt[0])
print("B = ", rslt[1])
The variable was declared inside a function making it a local variable and as such it can"t be accessed outside the function.
The variable is declared outside of the function making it a global variable and is not in anyway tied to your function.
The concept of attributes relates to Classes and you are dealing with a function so you might have to treat it as a class object.
If you are concerned bout accessing the local variables, you might as well do this:
y.py
from x import *
d = func() # func returns a list which is now
# identified/referenced by variable d
# displays the elements in the list using index position
print(d[0])
print(d[1])
If you want to use attributes, you may create a callable class instead of function:
class Func:
def __init__(self):
self.b = 10
self.a = 15
def __call__():
return [self.a, self.b]
func = Func()
Python has the concept of the scope. Local variables have no effect outside the function.
If you want to use it, use class and self or make getter function(but it's not Pythonic).
x.py
class X:
def __init__(self):
self.b = 10
self.a = 15
self.c = 20
def func(self):
return [self.a, self.b]
y.py
from x import X
x = X()
print(x.c)
print(x.func()[1])

Call nested function from another function in python?

I have a function inside inside another and a third function. How can I call my nested function inside of my third function? Is there any special libraries I can use? I am not allowed to edit a() or b(), only c().
def a():
def b():
print("hi")
def c():
# code only here to call b() to print
When you do this, function b is defined locally within a. This means that it cannot be accessed by default outside of a. There are two main ways to solve this, but both involve modifying a:
The global keyword (not recommended)
def a():
global b
def b():
print("hi")
Here the global keyword sets b up as a global variable, so that you can then access it by calling it normally from within c. This is generally frowned upon.
Returning the function from a and passing it to c
def a():
def b():
print("hi")
return b
def c(b):
#your code
Then, when you call c, you should pass b to it, which a will have returned. You can either do so thus:
b = a()
c(b)
Or you can simply call a every time you call c, thus:
c(a())
If you choose to do this, you can then define c thus:
def c():
b = a()
#your code here
which would allow you to simply call c normally, thus:
`c()`
This is not possible due to the way that Python scope works. b() is local to a(), and so does not exist within c().
EDIT: commenter is correct, the suggestion I initially gave doesn't work -- so this definitely just isn't possible.
As explained here, you cannot directly call a function included inside another. But, you can play around a bit and make it indirectly possible.
You could try calling functions via arguments and parameter values to call the function you want inside another function.
def a(invoke=None):
def b():
print('b')
if invoke == 'b':
b()
def c():
a(invoke='b')
c()
The result is:
b
You can even pass arguments to these function if you want:
def a(invoke=None, a_parameter=None):
def b(b_parameter=None):
if b_parameter == None:
print('b')
if b_parameter == 'b1':
print('b1')
if b_parameter == 'b2':
print('b2')
if invoke == 'b':
b(b_parameter = a_parameter)
def c(parameter=None):
a(invoke='b', a_parameter = parameter)
c()
Result:
b
But:
c('b1')
Result:
b1

Alternate to global variable in Python

I am implementing a recursive function in which I need to remember a global value. I will decrement this value in every recursive call and want it to reflect in other recursive calls also.
Here's a way I've done it.
First way:
global a
a = 3
def foo():
global a
if a == 1:
print 1
return None
print a
a -= 1 # This new 'a' should be available in the next call to foo()
foo()
The output:
3
2
1
But I want to use another way because my professor says global variables are dangerous and one should avoid using them.
Also I am not simply passing the variable 'a' as argument because 'a' in my actual code is just to keep track of some numbers, that is to track the numbering of nodes I am visiting first to last. So, I don't want to make my program complex by introducing 'a' as argument in every call.
Please suggest me whatever is the best programming practice to solve the above problem.
Don't use a global; just make a a parameter to the function:
def foo(a):
print a
if a == 1:
return None
foo(a-1)
foo(3)
Try this :
Use a parameter instead of a global variable.
Example code
a = 3
def foo(param):
if param == 1:
print 1
return None
print param
foo(param - 1)
foo(a)

What is the Pythonic way to avoid reference before assignment errors in enclosing scopes?

I'm speaking about the general case. Here's an example:
c = 1
def a():
def b():
print(c)
b()
c = 2
a()
This code will return the following error:
NameError: free variable 'c' referenced before assignment in enclosing scope. While the logical assumption is that the output should be 1. What is the Pythonic solution to this issue? Use the global or nonlocal statements (which I don't like)? Maybe just avoid such situations, where multiple scopes share variables with identical names?
Passing it as a parameter
When passing a outside variable as a parameter, avoid reusing names unless it's not possible that this variable can handle any other variable as parameter, then it doesn't really matter otherwise it will be confusing if you pass d the next time and you do operations on c within the function.
Secondly, the value of c will not get modified within the function even if changing name from param to c (it has very little meaning) when passing as a variable because it's not considered as a global varaible, even tho the variable is an object it will only be a object in this function unless you pass it into a class.
c = 1
def a(param):
def b():
print(param)
b()
param = 2
a(c)
You would need to stick to the global option if you don't want to pass it as a parameter and you still want to affect c outside of your function.
The global option will affect the "outside" c variable as your want it to.. but this is not really considered best practice, avid it if possible.
c = 1
def a():
global c
def b():
print(c)
b()
c = 2
a()
Here's what i would recommend:
c = 1
def a(param):
def b():
print(param)
b()
param = 2
return param
c = a(c)
Or even:
c = 1
def b(param):
print(param)
def a(param):
b(param)
param = 2
return param
c = a(c)

Can anyone help me understand Python variable scoping?

I wrote a test program that looked like this:
#!/usr/bin/python
def incrementc():
c = c + 1
def main():
c = 5
incrementc()
main()
print c
I'd think that since I called incrementc within the body of main, all variables from main would pass to incrementc. But when I run this program I get
Traceback (most recent call last):
File "test.py", line 10, in <module>
main()
File "test.py", line 8, in main
incrementc()
File "test.py", line 4, in incrementc
c = c + 1
UnboundLocalError: local variable 'c' referenced before assignment
Why isn't c passing through? And if I want a variable to be referenced by multiple functions, do I have to declare it globally? I read somewhere that global variables are bad.
Thanks!
You're thinking of dynamic scoping. The problem with dynamic scoping is that the behavior of incrementc would depend on previous function calls, which makes it very difficult to reason about the code. Instead most programming languages (also Python) use static scoping: c is visible only within main.
To accomplish what you want, you'd either use a global variable, or, better, pass c as a parameter. Now, because the primitives in Python are immutable, passing an integer can't be changed (it's effectively passed by value), so you'd have to pack it into a container, like a list. Like this:
def increment(l):
l[0] = l[0] + 1
def main():
c = [5]
increment(c)
print c[0]
main()
Or, even simpler:
def increment(l):
return l + 1
def main():
c = 5
print increment(c)
main()
Generally, global variables are bad because they make it very easy to write code that's hard to understand. If you only have these two functions, you can go ahead and make c global because it's still obvious what the code does. If you have more code, it's better to pass the variables as a parameter instead; this way you can more easily see who depends on the global variable.
When a variable is assigned to in a scope, Python assumes it's local for the whole scope unless you tell it otherwise.
So, to get this to work as you think it will, you need to use two global statements:
#!/usr/bin/python
def incrementc():
global c
c = c + 1
def main():
global c
c = 5
incrementc()
main()
print c
Otherwise, you're talking about a local variable named c in both situations.
The normal way to solve this, however, does not involve globals.
#!/usr/bin/python
def incrementc(c):
c = c + 1
return c
def main():
c = 5
c = incrementc(c)
return c
c = main()
print c
Here, in each function and in the global scope, c refers to a different variable, which you are passing around as an argument and with return values. If you wanted only one c, use a class:
class Foo:
def __init__(self, c):
self.c = c
self.incrementc()
def incrementc(self):
self.c = self.c + 1
foo = Foo(5)
print foo.c
The variable c isn't passing through because you do not hand any reference to c to the function incrementc.
What you're looking at here are 3 scopes, the global scope and those within the functions main and incrementc. In main you've properly defined a variable c, but increment c has no knowledge of this - so attempting to increment it is going to fail. Even if those two functions succeeded, trying to print c would fail in the global scope because it has no knowledge of the c you've defined in main.
You have a few options. One way to do this:
def incrementc(c):
c = c + 1
return c
def main():
c = 5
c = incrementc(c)
return c
c = main()
print c
Notice how c is being handed around. Of course, the name doesn't have to be preserved, you very well could write it like this:
def increment(z):
z = z + 1
return z
def main():
bar = 5
bar = increment(bar)
return bar
foo = main()
print foo
Another option that many would probably dislike (for good reason) is to use globals. In that case:
def incrementc():
global c # indicate intention to write to this global, not just read it
c = c + 1
def main():
global c # declares c in global space
c = 5
incrementc()
main()
print c
Any function in which you hope to MODIFY the GLOBAL instance of c, you need to inform the function. So you state, 'global c'. You can READ from the global without doing so. This would ensure (to some extent) that you don't make a mistake and overwrite a value in the global space unintentionally with a similar name, should you decide to use one in the local space of a function.
Hopefully that's clear enough, but feel free to ask for clarification on any point (I'm also open to being corrected if I've mistakenly described any part of this).
Global variables are bad.
Just like friends and enemys. Keep your friends close but keep your enemys even closer.
The function main last a local variable c, assignment the value 5
You then call the function inc..C. The c from main is now out of scope so you are trying to use a value of c that is not in scope - hence the error.

Categories

Resources