0

I have a Python file from which I would like to get all functions. I want to pass these functions to another file in order to collect data about these functions. For Example.py:

class Example:

    def method_to_extract(name: str) -> none:
        print(name)

I want to extract this method as an object through the file name and pass it as an argument to another function, like

func = magic_method(Example.py) # returns method_to_extract as a callable object
collect_data(func)

Is this possible? So far, I have only been able to extract the names of functions as strings. However, that does not help me, as I want to collect data about the functions like the number of arguments, type hints, and more, and therefore need to be able to access the entire function. I also tried getting the function using func = getattr(class, funcname), but I wasn't able to pass func as a parameter and extract the data I need.

Jakob0403
  • 3
  • 3

2 Answers2

1

Something like this:

from Example import Example.method_to_extract as mte
collect_data(mte)

The code you show is not entirely correct; you would have to make method_to_extract a @staticmethod if it's inside the Example class. Perhaps a better solution then would be to define it outside the class; or define a @classmethod which you can call to return a method of that class as an object. But both of these are generally best avoided; your class should probably simply have a method which implements collect_data, and your code should simply call that instead.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks! I agree, the solution is not perfect. However, I can't add a method collect_data to the class, as the code I want to extract the data from is not mine. Ideally, I want to have a class which gets a python file name as an argument, then collects data from the functions in that file, with the file name being variable. – Jakob0403 Jan 02 '21 at 14:22
  • You know about inheritance, right? You don't need to change the original code, just extend it. – tripleee Jan 02 '21 at 14:27
  • I considered that, but since I want my code to be flexible and work for any type of python file, I can't extend every single file I want to evaluate. – Jakob0403 Jan 02 '21 at 14:31
  • @Jakob0403 `any type of python file` I think you have an misunderstanding of Python's module system. – Ekrem Dinçel Jan 02 '21 at 14:35
1

I can think of 3 solutions, dependant on your particular needs.

  1. Simplest one: don't use classes, just global functions:
    # file1.py
    def method_to_extract(name):
        print(name)
    
    And then in another file just import and use it:
    # file2.py
    from file2 import method_to_extract
    method_to_extract()
    
  2. In case you especially want to use methods inside of a class, you can make them static with @staticmethod decorator:
    # file1.py
    class Example:
        @staticmethod
        def method_to_extract(name):
            print(name)
    
    And then in another file just import and use it:
    # file2.py
    from file2 import Example
    Example.method_to_extract()
    
    More on staticmetod decorator here: https://www.programiz.com/python-programming/methods/built-in/staticmethod
  3. Of course, not all of your methods can be static. Then you just have to create an instance of the class and then use its methods:
    # file1.py
    class Example:
        def method_to_extract(name):
            print(name)
    
    And then in another file just import and use it:
    # file2.py
    from file2 import Example
    instance = Example()
    instance.method_to_extract()
    

Basically, the above three approaches are in general only possible ways of accessing some particular function or method in python and the fact of exporting them to another file doesn't change a thing.

  • Thank you! But if I don't want the code to be static, rather handle any type of file given to it and extract data for the functions contained in that file, I can't use any of these solutions, or am I missing something here? Basically I want to be able to extract all methods from any python file and collect data about these methods. If you consider that I may not even know what is in the files I want to evaluate, this solution does not really work, unless I'm missing something here. – Jakob0403 Jan 02 '21 at 14:33
  • I would say it's a very weird use case. Maybe you will find something helpful here : https://stackoverflow.com/questions/1911281/how-do-i-get-list-of-methods-in-a-python-class – Szymon Kowaliński Jan 02 '21 at 14:45