About object reference and threads

Alex Martelli aleax at aleax.it
Mon Jan 27 14:12:11 EST 2003


Grumfish wrote:

> How do I tell a Thread object what Queue object to use? I have a
> parent object that has a Queue and a Thread as members. I want the
> parent object to create the thread, tell it to write to to the Queue
> object, then start the Thread. How do I do this? How do I get a
> reference to an object?

Any time you mention "an object", e.g. by mentioning the name of
a variable that refers to the object, what you do is exactly this:
you "get a reference to the object".  Python never makes a copy
unless you specifically ask for a copy (what it means to "ask for
a copy" may depend: e.g., a slice on a list asks for a copy, but
a slice on a Numeric.array doesn't).

So, just pass the Queue instance to some appropriate method of the Thread 
instance, e.g. the constructor; the method can assign its argument to some 
appropriate instance variable; and throughout this, it will always be a 
reference to the original Queue instance that gets assigned, and methods 
can later be directly called on it.


Alex





More information about the Python-list mailing list