简体   繁体   中英

Get the names of a Python function’s parameters when it is inside a decorator

i want to get the parameter names of a function while it is in a decorator function, I keep getting the function wrapper parameters that is inside the decorator and not the parameters of the original method

_component_name_does_not_exist_error is the decorator and create_queue_to_component is the method, i want to get at least the names component_name and queue_name

def _component_name_does_not_exist_error(func):
    def function_wrapper(self, component_name):
        if not self._does_component_exist(component_name):
            return self._create_response(
                False,
                f"Component named {component_name} doesn't exist"
            )
        return func

    return function_wrapper

@_component_name_does_not_exist_error
def create_queue_to_component(self, component_name,
                              queue_name, queue_size=1):
    if self.components[component_name].does_queue_exist(queue_name):
        return self._create_response(
            False,
            f"Queue named {queue_name} already exist"
        )

    self.components[component_name].create_queue(queue_name=queue_name,
                                                 queue_size=queue_size)
    return self._create_response(
        True,
        f"The Queue {queue_name} has been created"
    )

i tried using these methods with no luck, all return component_name without the queue_name (to make the code below more clear, pipeline_manager is an object of the class containing the methods)

def get_method_parameters(self):
    print(inspect.signature(self.pipeline_manager.create_queue_to_component))
    print(self.pipeline_manager.create_queue_to_component.__code__.co_varnames)
    print(inspect.getfullargspec(self.pipeline_manager.create_queue_to_component))

Thank you for reading this and for your help :)

Use functools.wraps from functools module

import functools

def _component_name_does_not_exist_error(func):
    @functools.wraps(func)
    def function_wrapper(self, component_name):
        if not self._does_component_exist(component_name):
            return self._create_response(
                False,
                f"Component named {component_name} doesn't exist"
            )
        return func

    return function_wrapper

Then

print(inspect.signature(self.pipeline_manager.create_queue_to_component))

Gives you what I think you want, which is the argument names of the create_queue_to_component function.

This answer describes functools.wraps very nicely.

def _component_name_does_not_exist_error(func):
    @functools.wraps(func)
    def function_wrapper(self, *args, **kwargs):
        if not self._does_component_exist(kwargs['component_name']):
            return self._create_response(
                False,
                f"Component named {kwargs['component_name']} doesn't exist"
            )
        return func(self, *args, **kwargs)

    return function_wrapper

This works for me (Thank you @wstk for suggesting your answer and helping me reaching the answer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM