Search code examples
pythonstring-comparisonfunctools

Partially apply a string equality function


From what I understand I can compare strings with is and ==. Is there a way I can partially apply these functions?

For example:

xs = ["hello", "world"]
functools.filter(functools.partial(is, "hello"), xs)

Gives me:

functools.filter(functools.partial(is, "hello"), xs)
                                    ^
SyntaxError: invalid syntax

Solution

  • You could use operator.eq:

    import operator
    import functools
    xs = ["hello", "world"]
    functools.filter(functools.partial(operator.eq, "hello"), xs)
    

    yields

    ['hello']
    

    operator.eq(a, b) is equivalent to a == b.