GLOBAL variables in python
Mikael Olofsson
mikael at isy.liu.se
Tue Nov 7 07:39:52 EST 2000
On 07-Nov-00 Satish (python9999 at my-deja.com) asked:
> i have wriiten a small application in which i need to use the variable
> which is local to one of the functions in a class , but i am not able to
> access the variable.
>
> class myclass:
> def entry():
> entry1=GtkEntry(10)
> entry2=GtkEntry(10)
> ......
>
> a= myclass()
>
> def get():
> name=a.entry.entry1.get_text()
> return name
>
> here i am getting a error like
> name=a.entry.entry1.get_text()
> attribute error entry1
>
> can any one suggest solution for this problem..
> and i also have a doubt whether v have any GLOBAL variables in python??
On 07-Nov-00 Max Møller Rasmussen answered:
> You need to declare it global inside the method:
>
> a= myclass()
> def get():
> global a
> name=a.entry.entry1.get_text()
> return name
This answers the question, but the interpreter does not complain about
a, it complains about entry1. There is no attribute entry1 of the
method entry. I am not sure what Satish is trying to accomplish here,
partly becaus I do not know anything about Gtk, but I can see a number
of errors in the code. First, the method entry needs a reference to
the instance a, so something like this:
class myclass:
def entry(self):
entry1=GtkEntry(10)
entry2=GtkEntry(10)
......
Second, the variables entry1, entry2, ..., will dissapear after the call
a.entry(). Perhaps the following is what Satish wants:
class myclass:
def entry(self):
self.entry1=GtkEntry(10)
self.entry2=GtkEntry(10)
......
a= myclass()
def get():
a.entry()
name=a.entry1.get_text()
return name
Then again, I might be completely wrong...
/Mikael
-----------------------------------------------------------------------
E-Mail: Mikael Olofsson <mikael at isy.liu.se>
WWW: http://www.dtr.isy.liu.se/dtr/staff/mikael
Phone: +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date: 07-Nov-00
Time: 13:25:52
/"\
\ / ASCII Ribbon Campaign
X Against HTML Mail
/ \
This message was sent by XF-Mail.
-----------------------------------------------------------------------
More information about the Python-list
mailing list