I want to use some variables through the code, of course if the variable is global, it's ok. But I want to use functions, so I could just pass some parameters in my future work.
For example, this code is throwing an error:
def fun1():
print a_variable
def fun2(a_variable='hello, world'):
fun1()
fun2('hello, world')
Error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-42-31e8e239671e> in <module>()
5 fun1()
6
----> 7 fun2('hello, world')
<ipython-input-42-31e8e239671e> in fun2(a_variable)
3
4 def fun2(a_variable='hello, world'):
----> 5 fun1()
6
7 fun2('hello, world')
<ipython-input-42-31e8e239671e> in fun1()
1 def fun1():
----> 2 print a_variable
3
4 def fun2(a_variable='hello, world'):
5 fun1()
NameError: global name 'a_variable' is not defined
Since a_variable
is valid in fun2
, how come fun1
is not? How can I resolve this? I don't want to add additional parameters to fun1
.