| Reply | « Previous Thread | Next Thread » |
|
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.
|
|
Quote:
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()
Regards, |
|
Yes, it is working now. Thanks.
|
|
Join Date: Feb 2008
Posts: 2,543
Location: Bhavnagar, Gujarat, India
gaba88
Online
Forum Nokia Champion
|
|
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 |
|
Join Date: Nov 2007
Posts: 319
Location: Sertaozinho/Brazil
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
Code:
def func1():
print var
var = 1
func1()
#output is 1
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"
![]() 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 | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |
| 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 |