passing object between classes

D

daved170

Hi everybody,
I built my owen log obj as a class.
I'm passing it to another object (actually to a thread).
When I run my app it raise error at the line when I'm using that log
obj. is there any problem with the concept of passing object as I do
it?
How can I do that?

class A:
def foo1:
myLog = cLog()
myYhread = cThread(myLog)
myThread.start()

class cThread:
def __init__(self,in_myLog):
sel.LogObj = in_myLog

def run():
sel.LogObj.writeLine("HI")

thanks
Dave
 
D

daved170

Please always post real code and state the exact error you get. That will
maximise the chance that you get a useful answer.

The code you posted will fail for numerous reasons and I can't guess
whether you have a problem with indentation, a problem because you
misspelled 'self' as 'sel', or because you missed out the 'self' parameter
altogether on a method, or perhaps your LogObj simply doesn't have a
writeLine method.

Hi Duncan,
You are right, I should have put the entire code but my question was
more theroretical and I know that the code that I posted won't work.

Let me simplified my question, I need to be able to write to the same
log file from different classes. and even more, from different
threads.
Is there any existing python Log object that do so? I no, I created my
own Log object that only open file and write a line to it, how can I
make it be global? Should I use it as a static object? will it work?
(offcurse in the case of the threads I'll use utex)

Thanks again,
DaveD
 
J

Jean-Michel Pichavant

daved170 said:
Hi Duncan,
You are right, I should have put the entire code but my question was
more theroretical and I know that the code that I posted won't work.

Let me simplified my question, I need to be able to write to the same
log file from different classes. and even more, from different
threads.
Is there any existing python Log object that do so? I no, I created my
own Log object that only open file and write a line to it, how can I
make it be global? Should I use it as a static object? will it work?
(offcurse in the case of the threads I'll use utex)

Thanks again,
DaveD
I have a theoretical answer to your theoretical question :

Use the standard python module logging for logging activities.
http://docs.python.org/library/logging.html

It's very powerful, standard, thread safe and time-saving.

cheers,

Jean-Michel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top