0

I have this file with unit tests:

#file tests.py

class TestResults(TestBase): ...

class TestRegisteredFunctions(TestBase):
    """Tests functions registered dynamically."""

    def testNewFunction(self):
        """Server must register new functions."""
        # Register new function
        def square(num):
            return num * num
        self.server.register('square', square)

        res = self.jsonrpc_req(1, 'square', [2])
        self.assertEqual(res['result'], 4)

Most of the code is omitted because will not be relevant.

My problem is I need to grab that "square" function. For example doing getattr(tests, 'square') in another module would correctly get me that square function. How can I achieve such result?

walkman
  • 478
  • 1
  • 8
  • 21
  • 2
    by extracting it to the module/class scope – Azat Ibrakov May 02 '20 at 19:23
  • sorry, could you give more of an example? I'm quite new to python – walkman May 02 '20 at 19:27
  • Just wondering why would you want to do that? I believe that would defeat the main purpose of inner functions (protect them from everything happening outside of the function, meaning that they are hidden from the global scope). – goosfraba May 02 '20 at 19:35
  • I'm a student and the objective is to make all these unit tests pass. But one extra challenge is to get the parameter names of every function used. Because that function is inside all that stuff my code does not work for it but still it's used and I need to get her parameter names to get the extra points. That's basically it, this is roughly my second week of Python so I'm quite lost in this process @goosfraba – walkman May 02 '20 at 19:43
  • what does "to get the parameter names of every function used" mean? you need `square` function to be called or what? – Azat Ibrakov May 02 '20 at 19:47
  • I need to get the parameter names that function uses programmatically. That or any other that's inside the other classes @AzatIbrakov – walkman May 02 '20 at 19:49
  • why do you need that kind of introspection? – Azat Ibrakov May 02 '20 at 20:04
  • because I'm a student and the teacher asks for it... I got it to work with some ugly ifs just to pass the unit tests as I'm seeing that fetching all this will be more than I can handle – walkman May 02 '20 at 20:12

0 Answers0