Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: attributes on a generator object

Tags:

python

Is it possible to create an attribute on a generator object?

Here's a very simple example:

def filter(x):
    for line in myContent:
        if line == x:
            yield x

Now say I have a lot of these filter generator objects floating around... maybe some of them are anonymous... I want to go back later and interrogate them for what they are filtering for. Is there a way I can a) interrogate the generator object for the value of x or b) set an attribute with the value of x that I can later interrogate?

Thanks

like image 608
Josh Arenberg Avatar asked Jun 05 '09 15:06

Josh Arenberg


1 Answers

Yes.

class Filter( object ):
    def __init__( self, content ):
        self.content = content
    def __call__( self, someParam ):
        self.someParam = someParam
        for line in self.content:
            if line == someParam:
                yield line
like image 67
S.Lott Avatar answered Oct 30 '22 20:10

S.Lott