Search Queue

Bill Scherer bill.scherer at verizonwireless.com
Tue Jan 16 10:40:01 EST 2007


abcd wrote:

>I have a class such as...
>
>id = 0
>class Foo:
>    def __init__(self, data):
>        self.id = id
>        id += 1
>        self.data = data
>
>And I am storing them in a Queue.Queue...
>
>import Queue
>q = Queue.Queue()
>q.put(Foo('blah'))
>q.put(Foo('hello world'))
>q.put(Foo('test'))
>
>how can I search "q" for an instance of Foo which has 'id' equal to say
>2?  Typically a queue only lets you put and get, not really search.
>
>Thanks.
>
>  
>
You could iterate through q.queue. Something like:

for instance in q.queue:
    if instance.id == 2:
        print 'found it!"


But, you mess around with the internals of a Queue instance at your own 
peril!





More information about the Python-list mailing list