Help needed - function apparently global cannot be called.

Ian Hobson ian at ianhobson.co.uk
Tue Sep 7 06:18:10 EDT 2010


Hi all you experts,

This has me beat. Has anyone any ideas about what might be going wrong?

This is code from within a windows service (hence no print statements - 
no sys.stdout to print on!).

I am trying to trace through to find where the code is not working. No 
stdout so I have to log to a file.

I have the following code fragments.

def log(message):
     f = open('d:\logfile.txt','a')
     f.write(message + "\n")
     f.close()

from DelNotePrinter import DelNotePrinter

note the order of the above - log is defined before the import.
Later in the  source I have

         log('disPrint is:'+disPrint)
         log('count is:'+count)
         log(repr(DelNotePrinter))
         printer = DelNotePrinter(disPrint,int(count))

The DelNotePrinter.py file cannot us log even though it is declared
as global. The code is...

# coding=utf8
#    DelNotePrinter = code to print delivery notes
   assorted imports removed for space reasons

class DelNotePrinter(object):
     ''' Print Delivery Note on A5 in portrait '''
     def __init__(self,printer,copies):
         ''' create printer and painter '''
         global font,sm,log
         log('DelNotePrinter: starting')
         self.printer = QPrinter(QPrinter.HighResolution)
etc

The file the log writes contains..
disPrint is:HP Deskjet 6940 series
count is:1
<class 'DelNotePrinter.DelNotePrinter'>

The > is followed by a newline and end of file! Where is the 
DelNotePrinter: starting message?

If I replace the opening of __init__ with
	global font,sm,log
         f = open('d:\logfile.txt','a')
         f.write('DelNotePrinter: starting' + "\n")
         f.close()
         self.printer = QPrinter(QPrinter.HighResolution)

then the message IS written to the log file.

I have read http://docs.python.org/reference/simple_stmts.html#global 
very carefully and I still don't understand.

Regards

Ian

p.s. There are no trackbacks, no events on the event log, and no error 
messages I have been able to find. When run as a script (not a service) 
DelNotePrinter does produce the expected printout.




More information about the Python-list mailing list