<div dir="ltr"><div><div><div><div>Hi,<br><br></div>I&#39;ve been taking a Python class from OReilly School of Technology since about November or so.  I&#39;m finally on the last of four courses, and this course is called Advanced Python.  I don&#39;t feel very advanced!  But this assignment is about using __magic__ methods in a composable class, and I&#39;ve got a problem.<br><br></div>Here&#39;s the class from a file called composable.py:<br><br></div><div>*** snip ***<br></div><div><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></div><div>*** snip ***<br></div><div><br></div>The test is test_composable.py:<br><br></div>*** snip ***<br><div><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 pow(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):<br>            b = pow(pow(a,a), pow(a,a))<br>            square1 = Composable(pow)<br>            square2 = Composable(pow)<br>            # should equal pow(pow(x,x), pow(x,x))<br>            xtimespwr = pow(square1(a,a), square2(a,a))<br>            self.assertEqual(xtimespwr, b)<br>        with self.assertRaises(ValueError):<br>            oops = square1(2,-2)<br>        with self.assertRaises(TypeError):<br>            oops = square1(3, &#39;a&#39;)<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>        with self.assertRaises(TypeError):<br>            fc = fc * 3<br>        with self.assertRaises(TypeError):<br>            fc = square * fc<br><br>if __name__ == &quot;__main__&quot;:<br>    unittest.main()<br><br></div><div>*** snip ***<br><br></div><div>Everything passes except the ValueError test in test_pow. (When I comment the ValueError test out, the unittest passes.) I should think I was triggering that special __pow__ function, but I might be triggering the built-in pow().    Any insights on why the ValueError isn&#39;t getting triggered?<br><br></div><div>Thanks!<br></div><div>Dave<br></div><div><br></div></div>