[Tutor] Another question on global variables...
andy surany
mongo57a@comcast.net
Wed, 16 Oct 2002 23:01:22 -0400
This is a multi-part message in MIME format.
--Boundary_(ID_t0Rz2xnpFd/Q4gHCniDxBw)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT
I know that nobody likes 'em.... but,
If I define a global variable at the beginning of my program, like:
global abc
abc=[]
Then assign it like:
Class numberone:
def set_it:
abc=['1','2']
# This assignment works fine and the variable is used throughout the class.
Then reference it like:
Class numbertwo:
def ref_it
size=len(abc)
# Here, it appears that abc is [] so the length is zero. Should be 2
Why doesn't this work? Are global assignments unique to the class which made the assignment? Or more specifically, do I need to reference a global based on class, such as numberone.abc?? (or???)
And being somewhat ignorant of programming, if I drop the whole idea of globals and try to use a function, how would it work? I can certainly set up a function to assign the variable, like:
def assign(request_type):
If request_type==obtain:
abc = askstring.........
return abc
Once I leave the function, isn't abc a null variable? So I set it on the first call, but just want it to continue to pass me the value previously set on subsequent calls. Am I anywhere close?????
TIA.
--Boundary_(ID_t0Rz2xnpFd/Q4gHCniDxBw)
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7BIT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT size=2>I know that nobody likes 'em.... but,</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>If I define a global variable at the beginning of my program,
like:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> global abc</FONT></DIV>
<DIV><FONT size=2> abc=[]</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Then assign it like:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Class numberone:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> def set_it:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>
abc=['1','2']</FONT></DIV>
<DIV><FONT size=2> # This assignment works
fine and the variable is used throughout the class.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Then reference it like:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Class numbertwo:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> def ref_it</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>
size=len(abc)</FONT></DIV>
<DIV><FONT size=2> # Here, it appears that
abc is [] so the length is zero. Should be 2</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Why doesn't this work? Are global assignments unique to the
class which made the assignment? Or more specifically, do I need to reference a
global based on class, such as numberone.abc?? (or???)</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>And being somewhat ignorant of programming, if I drop the
whole idea of globals and try to use a function, how would it work? I can
certainly set up a function to assign the variable, like:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>def assign(request_type):</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> If request_type==obtain:</FONT></DIV>
<DIV><FONT size=2> abc =
askstring.........</FONT></DIV>
<DIV><FONT size=2> return abc</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Once I leave the function, isn't abc a null variable? So I set
it on the first call, but just want it to continue to pass me the value
previously set on subsequent calls. Am I anywhere close?????</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>TIA.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>
</FONT></DIV></BODY></HTML>
--Boundary_(ID_t0Rz2xnpFd/Q4gHCniDxBw)--