<div dir="ltr"><div><div><div><div><div><div><div><div>So, when I first read the lesson and did the assignment, I was very sleep deprived.  My tests and methods were pretty convoluted.  I cleaned up the tests, and I think they&#39;re passing now.  I&#39;m not sure why, but I moved the tests for Composable(pwr) down to the test_exceptions section, the tests passed.  And everything looked less &quot;Sleep Deprived&quot;!  :-)<br><br></div>But, any hints or tips?  I am wide open to improvement here.  <br><br></div>Thanks!<br><br></div>*** composable.py ***<br>#!/usr/bin/env python3<br><br>&quot;&quot;&quot;<br>composable.py: defines a composable function class.<br>&quot;&quot;&quot;<br><br>import types<br><br>class Composable:<br>    def __init__(self, f):<br>        &quot;Store reference to proxied function.&quot;<br>        self.func = f<br><br>    def __call__(self, *args, **kwargs):<br>        &quot;Proxy the function, passing all arguments through.&quot;<br>        return self.func(*args, **kwargs)<br><br>    def __pow__(self, other):<br>        &quot;Return the composition of x to the power of y&quot;<br>        if other &gt; 0:<br>            for i in range(other-1):<br>                self *= self <br>            return self<br>        else:<br>            raise ValueError(&quot;Needs to be positive integer.&quot;)<br><br><br>    def __mul__(self, other):<br>        &quot;Return the composition of proxied and another function.&quot;<br>        if type(other) is Composable:<br>            def anon(x):<br>                return self.func(other.func(x))<br>            return Composable(anon)<br>        elif type(other) is types.FunctionType:<br>            def anon(x):<br>                return self.func(other(x))<br>            return Composable(anon)<br>        raise TypeError(&quot;Illegal operands for multiplication&quot;)<br><br>    def __repr__(self):<br>        return &quot;&lt;Composable function {0} at 0x{1:X}&gt;&quot;.format(self.func.__name__, id(self))<br><br><br></div>*** snip: end of composable.py  ***<br><br><br></div>*** snip: beginning of test_composable.py  ***<br><br>#!/usr/bin/env python3<br><br>&quot;&quot;&quot;<br>test_composable.py: performs simple tests of composable functions.<br>&quot;&quot;&quot;<br><br>import unittest<br>from composable import Composable<br><br>def reverse(s):<br>    &quot;Reverses a string using negative-stride sequencing.&quot;<br>    return s[::-1]<br><br>def square(x):<br>    &quot;Multiplies a number by itself.&quot;<br>    return x*x<br><br>def pwr(x, y):<br>    &quot;raise x to the power of y&quot;<br>    return x**y<br><br><br>class ComposableTestCase(unittest.TestCase):<br><br>    def test_inverse(self):<br>        reverser = Composable(reverse)<br>        nulltran = reverser * reverser<br>        for s in &quot;&quot;, &quot;a&quot;, &quot;0123456789&quot;, &quot;abcdefghijklmnopqrstuvwxyz&quot;:<br>            self.assertEqual(nulltran(s), s)<br><br>    def test_pow(self):<br>        a = 0<br>        for a in (2,3,4,5,6):<br>            raizit = Composable(pwr)<br>            cuber = raizit(a, 3)<br>            b = a * a * a<br>            self.assertEqual(cuber, b)<br><br>    def test_square(self):<br>        squarer = Composable(square)<br>        po4 = squarer * squarer<br>        for v, r in ((1,1), (2, 16), (3, 81)):<br>            self.assertEqual(po4(v), r)<br><br>    def test_exceptions(self):<br>        fc = Composable(square)<br>        pc = Composable(pwr)<br>        with self.assertRaises(TypeError):<br>            fc = fc * 3<br>        with self.assertRaises(TypeError):<br>            fc = square * fc<br>        with self.assertRaises(ValueError):<br>            pc = pc ** -3<br>        with self.assertRaises(TypeError):<br>            pc = pc ** &#39;a&#39;<br><br>if __name__ == &quot;__main__&quot;:<br>    unittest.main()<br><br></div>*** snip: end of test_composable.py ***<br><br></div>Thanks for any help!!<br><br></div>Dave<br></div>