Can we make a local variable in a function as global variable???

7stud bbxx789_05ss at yahoo.com
Thu Apr 5 11:40:13 CEST 2007


On Apr 5, 3:19 am, "sairam" <sai... at gmail.com> wrote:
> I have some local variables defined in one method and Can I make those
> variables as global variables? If so , can any one explain me how can
> I do that
>
> Thanks,
> Sairam
-----------
num = None

def f1():
    global num
    num = 30

def f2():
    print num


f1()
f2()
-----------
You can read from a global variable, but to assign to one, you have to
declare its name in a global statement on the first line of the
function.

A better way:
------------
def f1():
    num = 30
    return num

def f2(x):
    print x

result = f1()
f2(result)
-----------




More information about the Python-list mailing list