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

S

sairam

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
 
7

7stud

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)
-----------
 
S

Steve Holden

Collin said:
As for me, I find this problem annoying, but easy to solve. My solution is:


To set global variable spam to 4, I say:


This always works, and is much more convenient than:


and then worry about local variables also named spam.
That's truly horrible. And what if you have a local variable called "this"?

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top