passing object between classes

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Sep 21 17:36:18 CEST 2009


daved170 wrote:
> On Sep 21, 1:44 pm, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
>   
>> daved170 <daved... at gmail.com> wrote:
>>     
>>> 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
>>>       
>> 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.
>>
>> --
>> Duncan Boothhttp://kupuguy.blogspot.com- Hide quoted text -
>>
>> - Show quoted text -
>>     
>
> 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



More information about the Python-list mailing list