title: Pointfree function combination in Python
body: <p>I have some predicates, e.g.:</p>
  
  <pre><code>is_divisible_by_13 = lambda i: i % 13 == 0
  is_palindrome = lambda x: str(x) == str(x)[::-1]
  </code></pre>
  
  <p>and want to logically combine them as in:</p>
  
  <pre><code>filter(lambda x: is_divisible_by_13(x) and is_palindrome(x), range(1000,10000))
  </code></pre>
  
  <p>The question is now: Can such combination  be written in a <a href="https://en.wikipedia.org/wiki/Tacit_programming" rel="nofollow noreferrer">pointfree</a> style, such as:</p>
  
  <pre><code>filter(is_divisible_by_13 and is_palindrome, range(1000,10000))
  </code></pre>
  
  <p>This has of course not the desired effect because the truth value of lambda functions is <code>True</code> and <code>and</code> and <code>or</code> are short-circuiting operators. The closest thing I came up with was to define a class <code>P</code> which is a simple predicate container that implements <code>__call__()</code> and has the methods <code>and_()</code> and <code>or_()</code> to combine predicates. The definition of <code>P</code> is as follows:</p>
  
  <pre><code>import copy
  
  class P(object):
      def __init__(self, predicate):
          self.pred = predicate
  
      def __call__(self, obj):
          return self.pred(obj)
  
      def __copy_pred(self):
          return copy.copy(self.pred)
  
      def and_(self, predicate):
          pred = self.__copy_pred()
          self.pred = lambda x: pred(x) and predicate(x)
          return self
  
      def or_(self, predicate):
          pred = self.__copy_pred()
          self.pred = lambda x: pred(x) or predicate(x)
          return self
  </code></pre>
  
  <p>With <code>P</code> I can now create a new predicate that is a combination of predicates like this:</p>
  
  <pre><code>P(is_divisible_by_13).and_(is_palindrome)
  </code></pre>
  
  <p>which is equivalent to the above lambda function. This comes closer to what I'd like to have, but it is also not pointfree (the points are now the predicates itself instead of their arguments). Now the second question is: Is there a better or shorter way (maybe without parentheses and dots) to combine predicates in Python than using classes like <code>P</code> and without using (lambda) functions?</p>