Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can we make a local variable in a function as global variable???

Reply
Thread Tools

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

 
 
sairam
Guest
Posts: n/a
 
      04-05-2007
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

 
Reply With Quote
 
 
 
 
7stud
Guest
Posts: n/a
 
      04-05-2007
On Apr 5, 3:19 am, "sairam" <(E-Mail Removed)> 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)
-----------

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      04-05-2007
Collin Stocks wrote:
> As for me, I find this problem annoying, but easy to solve. My solution is:
>
> >>> this=__import__(__name__)

>
> To set global variable spam to 4, I say:
>
> >>> this.spam=4

>
> This always works, and is much more convenient than:
>
> >>> global spam
> >>> spam=4

>
> 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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.9 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? PerlFAQ Server Perl Misc 0 01-12-2011 11:00 PM
Puzzling: local variable in recursive function made global? Daniel Oberski Python 9 04-03-2009 03:02 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Accessing a global variable when there is a local variable in the same name Mohanasundaram C Programming 44 08-23-2004 11:17 PM



Advertisments