Instructing a function to be called with a specific argument without executing it

前端 未结 2 1449
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 12:44

I am trying to encode logic to filter a Pandas dataframe. I\'d like to encode the logic as a dictionary, with the subgroup name as the key, and the function to filter for the su

2条回答
  •  悲哀的现实
    2021-01-24 13:39

    You can consider functools.partialmethod, which allows you to specify any number of args or kwargs:

    from functools import partialmethod
    
    mappings = {'Jets Fans': partialmethod(BaseFilter.for_jets_fans, 'Jets'),
                'Patriots Fans': partialmethod(BaseFilter.for_patriots_fans, 'Patriots'),
                'NFC Fans': partialmethod(BaseFilter.for_team_fans, 'Bears', 'Packers')}
    

提交回复
热议问题