9876543210

9876543210

Well it works exactly like all other module calls.

Supposing that your module is called CodeIncludes and the class is SomeClass then:


include CodeIncludes
instance_of_SomeClass = CodeIncludes.SomeClass()

Simple! :smiley:

Hmm.

except isn’t it ? :


<b>import</b> CodeIncludes
instance_of_SomeClass = CodeIncludes.SomeClass()

:confused: :confused:

Mike

yes

/Nathan

9876543210

I’m not sure I know what you want to do but I think this might work:

Somewhere near the top of your code:
logger = None

Later in the code:
if logger not None:
>>>>self.logger = logger

You need to pass the ‘debug-LOGGER class’ instance to the imported module when you call it.
I’ll work on an example…

I know it’s a double post, but I think it’s forgivable.

#!/usr/bin/python
import os

class debugLogger:
	def __init__(self,file='%s/test.log' %os.getcwd(),debugLevel=0):
		self.pipe=open(file,'w')
		self.debugLevel=debugLevel
		self.line=0

	def log(self,string):
		self.pipe.write('%s:%s
'%(self.line,string))
		print '%s: I logged:- %s' %(self.line,string)
		self.pipe.flush()
		self.line+=1

class someclass:
	def __init__(self,logger=None):
		self.debug=logger
			
	def someFunction(self):
		pass
		#some stuff, maybe even useful
		if self.debug: self.debug.log('I did something you should know about!')

class someclass2:
	def __init__(self,logger=None):
		self.debug=logger
			
	def someFunction(self):
		pass
		#some stuff, maybe even useful
		if self.debug:
			if self.debug.debugLevel&gt;1:self.debug.log('I did something you may want know about!')

def main():
	out=debugLogger('/tmp/logfile',2)
	function=someclass(out)
	newFunction=someclass2(out)
	function.someFunction()
	newFunction.someFunction()
	newFunction.someFunction()
	function.someFunction()
	out.pipe.close()

main()

Any of the three defined classes could have been imported, and the logger class is just to for proof of concept. You can tell from the line numbers in the log file that someclass and someclass2 are both using the same instance of the logger, and are also written with a fallback that if you don’t pass the logger class to them on calling them, they won’t error out.

The original instance of the logger is called from main(), and as long as main() is only called once, will be the same logger passed to any class you see fit.
Don’t forget to close the pipe at the end of the script.
HTH

9876543210