Skip to content

Is there any way to get the "filter name" inside the common custom filter function defined against it #1840

Discussion options

You must be logged in to vote

No, that name isn't even known to Python itself since it's not the function name but just some dict key it's assigned to.

Here's a workaround:

filters = { 
    "filter1": common_custom_filter_func, 
    "filter2": common_custom_filter_func, 
}
filters = {k: functools.partial(v, k) for k, v in filters.items()}

Then just add a new positional argument to be beginning of your function, e.g. def common_filter(name, whatever, else) and use that argument to get the name.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by davidism
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1839 on April 11, 2023 16:04.