You Are Here:

Community: Developer Discussion Boards

#1 Old handling global variables - 2009-03-04, 13:59

Join Date: Feb 2009
Posts: 11
vijaynag
Offline
Registered User
Hi, I am currently facing issues handling global variable. See the code below.

global var

def fun1:
var = appuifw.query(u"Enter your name","text")
print var


def fun2:
print var

def fun3:
print var

fun1()
fun2()
fun3()

------------------------

the value of var is not taken global and in fun2 and fun3 value of var is not printed.
If I try to use Var in fun2 and fun3, it says var is not defined.
please help
Last edited by vijaynag : 2009-03-04 at 14:18.
Reply With Quote

#2 Old Re: handling global variables - 2009-03-04, 14:21

Join Date: Jul 2008
Posts: 103
ck.umraliya's Avatar
ck.umraliya
Offline
Forum Nokia Champion
Quote:
Originally Posted by vijaynag View Post
the value of var is not taken global and in fun2 and fun3 value of var is not printed.
If I try to use Var in fun2 and fun3, it says var is not defined.
please help
Hello,

When a variable is to be used outside its scope, you need to declare it global like below.

Code:
def fun1():
    global var
    var = 5
    print var


def fun2():
    print var

def fun3():
    print var

fun1()
fun2()
fun3()
this will print var three times.

Regards,
Reply With Quote

#3 Old Re: handling global variables - 2009-03-04, 14:44

Join Date: Feb 2009
Posts: 11
vijaynag
Offline
Registered User
Yes, it is working now. Thanks.
Reply With Quote

#4 Old Re: handling global variables - 2009-03-04, 15:44

Join Date: Feb 2008
Posts: 2,543
Location: Bhavnagar, Gujarat, India
Send a message via Yahoo to gaba88 Send a message via Skype™ to gaba88
gaba88's Avatar
gaba88
Online
Forum Nokia Champion
Quote:
Originally Posted by vijaynag View Post
Yes, it is working now. Thanks.
hello vijaynag

its great that you solved your problem.
But then also i will advice you to have a glance on this wiki article about using variables in Python.


Gargi Das- http://gargidas.blogsot.com

Forum Nokia Python Wiki


Learn Python at http://mobapps.org/PyS60
Reply With Quote

#5 Old Re: handling global variables - 2009-03-05, 22:07

Join Date: Nov 2007
Posts: 319
Location: Sertaozinho/Brazil
Send a message via MSN to marcelobarrosalmeida Send a message via Skype™ to marcelobarrosalmeida
marcelobarrosalmeida's Avatar
marcelobarrosalmeida
Offline
Forum Nokia Champion
Hi

Just an aside.

When you make a assignment to var without global keyword, Python interpreter thinks this var is local and your other functions will not see any update to it since its value will be lost when the interpreter leaves func1 scope.

Code:
def func1():
    var = 2
    print var

var = 1
func1()
# output is 2
However, if you have just used var (no assignments), Python interpreter uses it as global. I mean:

Code:
def func1():
    print var

var = 1
func1()
#output is 1
So, global keyword may be omitted in this case.

But, how about the following code:

Code:
def func1():
    print var
    var = 2
    print var

var = 1
func1()
# output is "UnboundLocalError: local variable 'var' referenced before assignment"
In this case, Python does not treat first var as global, as you could expect, since it knows that func1 uses var some lines ahead. So, var is local and it was not assigned before (error) ! And, yes, Python process the function before executing it.

However, to avoid problems (explicit is better than implicit), put a big global at beginning.

Marcelo Barros


Marcelo Barros
Nokia E71, N800, N95 and XM 5800
http://www.croozeus.com
http://wordmobi.wordpress.com
http://jedizone.wordpress.com
Last edited by marcelobarrosalmeida : 2009-03-06 at 06:44.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with const global variables in GCCE build xunhuan General Symbian C++ 7 2007-07-23 08:15
global variables and GCCE tmfelser General Symbian C++ 2 2007-06-12 11:37
Global variables??? leomil General Symbian C++ 2 2006-03-08 09:24
About static and global variables kenwc General Symbian C++ 0 2005-05-05 04:48
global variables Nokia_Archive General Symbian C++ 1 2002-06-02 05:20

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia