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.

5
  • You'll need to actually import Example.py before being able to use getattr() or similar methods. If you need to you can do imports dynamically with importlib Commented Jan 2, 2021 at 14:04
  • it would be easier if you want to extract the functions from the class instead of the file Commented Jan 2, 2021 at 14:11
  • How does this work if I want the file name to be a variable? Commented Jan 2, 2021 at 14:14
  • How would it work for extracting functions from a class instead of a file? Commented Jan 2, 2021 at 14:15
  • @AvenDesta Not really, both are easy to do. Commented Jan 2, 2021 at 14:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
I would say it's a very weird use case. Maybe you will find something helpful here : stackoverflow.com/questions/1911281/…
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.

4 Comments

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.
You know about inheritance, right? You don't need to change the original code, just extend it.
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 any type of python file I think you have an misunderstanding of Python's module system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.