0

I'm looking for a function or a method that can return a list of parameters' names of a type object, something like: func(X) or X.method that will return ['normType', 'crossCheck'] for cv2.BFMatcher and ['index_params', 'search_params'] for cv2.FlannBasedMatcher

(and even the keys of index_params and search_params dictionary if possible, like ['algorithm', 'table_number', 'key_size', 'multi_probe_level'] etc.)

I tried some inspect methods like inspect.getfullargspec but they don't work on type objects

Use case:

I'm writing a function that based on its arguments will create a combination (detector, descriptor, matcher), and i want to pass the parameters to use for those three directly when i call the function.

and i want to use something like dict(zip(params_keys, params_values))

so I'm thinking about something like this:

def method(detector_name, detector_params_values, descriptor_name, descriptor_params_values, matcher_name, matcher_params_values):
    # ...
    MATCHERS = {"BF": cv2.BFMatcher, "FLANN": cv2.FlannBasedMatcher}
    matcher_params_keys = func(matcher_name)  # this is what i'm looking for
    matcher = MATCHERS[matcher_name](**dict(zip(matcher_params_keys, matcher_params_values)))
    
    # and basically the same for the detectors and descriptors
    

PS: I may be using some wrong terminology because I'm not an expert in Python, but I hope my question is clear.

  • 2
    Smells like an XY-Problem to me. What's your use-case? What are you actually trying to do with those parameter name strings? Why do you think you need them? Unless you're doing something really esoteric, variable identifiers are only meaningful for the source code, not during script execution. – Paul M. Nov 23 '22 at 17:59
  • @PaulM. thank you for your answer, I added some explanation about the use case. – Zikromanti Nov 23 '22 at 18:26
  • @Barmar the answer I'm redirected to do not answer my question, I saw it before asking mine. Can you reopen the question please? I added some more explanation. Thanks – Zikromanti Nov 23 '22 at 21:02
  • Then I misunderstood what you're asking for. I thought you wanted to know the names of the arguments of a given function, that's what the linked question answers. – Barmar Nov 23 '22 at 21:04
  • This question is unclear to me. Will you describe what you are trying to accomplish in plain English? I mean what is the original problem that you are trying to solve with this approach? – Code-Apprentice Nov 23 '22 at 21:33
  • @Barmar was right. You are trying to find out all of the parameter names for a given function. What good would that do? Doing generic function calls is not generally that useful. The functions that are calling your `method` function will already need to know what parameters to pass in, right? So what have you gained? – Tim Roberts Nov 23 '22 at 21:40
  • @TimRoberts `cv2.BFMatcher` is not a ``, it's a ``, so the solution proposed in the linked answer does not work for me. if I remove the `matcher_params_keys` line from the code above, the user of my function will have to enter a dictionary with keys and values of the parameters they want to use. if I find what I'm looking for in this question, the user will have to enter only a list of values. so i think it's useful because it's easier to write only the values than to write both keys and values every time. – Zikromanti Nov 23 '22 at 21:54
  • @Code-Apprentice tell me if my question is still unclear after my last comment, so I can edit the question and add, what's the current solution, what i think is the problem, and what i hope I'll be able to do. Thanks – Zikromanti Nov 23 '22 at 22:00
  • So, if `N` is a class, then you need to know what parameters `N.__init__` takes. `N` might have lots of members that are not specified as inputs. Of course, many large classes just take `**kwarrgs` and validate within `__init__`, in which case the task is impossible. Python was not designed to be totally introspective, like C#. – Tim Roberts Nov 23 '22 at 22:34
  • Maybe `*args` and `**kwargs` will do what you want? This allows you to gether up parameters in a list or a dictionary. Likewise, you can expand arguments from a list or dictionary. I suggest you read more about these to see if they will help solve your problem. – Code-Apprentice Nov 24 '22 at 03:27

0 Answers0