Re: Simple Python problem...

michael shiplett (michael.shiplett@umich.edu)
Thu, 16 Feb 1995 05:29:37 -0500

"dh" == Douglas Harber <dh@triple-i.com> writes:

dh> ..but of course, I've been unable to solve it! I'm under the
dh> impression that I should be able to use a global value a function
dh> defined in a module, but for the life of me I can't get it to
dh> work! I know I must be missing something trivially stupid, so
dh> could somebody set me straight.

dh> In a module, say foo.py, I've got:

dh> import sys

dh> seqno = 0

dh> def SomeFunc():
dh> print 'Using serial #' + seqno
dh> seqno = seqno+1

For what you're trying to accomplish, you need to add a `global'
statement--and pass the representation of seqno to the print
statement or use the printf() support.

def SomeFunc():
global seqno
print 'Using serial #' + `seqno`
# print 'Using serial #%d' % seqno
seqno = seqno+1

If seqno were not a global variable, you would have have an
assignment of seqno before it could be used (otherwise seqno is not in
the namespace?). Here's this less interesting version of SomeFunc().

def SomeFunc():
seqno = 0
print 'Using serial #' + `seqno`
seqno = seqno+1

michael

[wheee, I answered a python question--maybe even correctly]