Python - overload same function with different arguments

Alicja Głowacka

I have got a function that sets features and want to have two versions of it. One takes all features and splits them into words and phrases and the second one receives already split words and phrases as arguments

def set_features_2(self, words, phrases):
    self.vocabulary = set(words)
    self.phrases = SortedSet(phrases)

def set_features(self, features):
    phrases = [f for f in features if ' ' in f]
    words = [f for f in features if f not in phrases]
    self.set_features_2(words, phrases)

What is the easiest way to remove this duplication? Both of them should be called "set_features" but both receive a different set of arguments. I know that it is possible to use args and kwargs but it is an overkill for such as trivial case.

Mureinik

You can't overload function arguments per se, but you can emulate this behavior with keyword arguments. The slightly annoying part is that you'd have to handle the validity checks (i.e., that the user doesn't pass both features and words and phases). E.g.:

def set_features(self, features = None, words = None, phrases = None):
    if features: 
        if words or phrases:
            raise ValueError('Either pass features or words and phrases')
        else:
            phrases = [f for f in features if ' ' in f]
            words = [f for f in features if f not in phrases]

    self.vocabulary = set(words)
    self.phrases = SortedSet(phrases)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Different arguments, but behavior of function is same

From Dev

Generate python function with different arguments

From Java

Jest mock the same function twice with different arguments

From Dev

Function returns different answers with same arguments

From Dev

Ruby threads calling the same function with different arguments

From Dev

How to parallelize the same function using different arguments?

From Dev

Two different arguments for the same function but return different results?

From Dev

Run a function in parallel with different arguments - python

From Dev

partial function overload in python

From Dev

Apply the same function multiple time with different sets of arguments

From Dev

CMAKE: repeated calls to function with different arguments give the same results

From Dev

Different arguments with same template

From Dev

How to overload function switching by argument functor arguments

From Dev

"No overload for function "instantiate" takes 4 arguments"

From Dev

Initialize two Python classes with same arguments, get different results

From Dev

Recursive function with same arguments

From Dev

Combine same function with different parameters - Python

From Dev

Same python function giving different output

From Dev

Python 3 - Multiprocessing same function with different args

From Dev

Best way to "overload" function in python?

From Dev

Can I overload REST url resource having different arguments?

From Dev

Same function with same signatures in the same class/struct? Overload?

From Dev

Python Using Keyword and variable number of arguments in same function

From Dev

Use same arguments with different command

From Dev

functions with different names but same arguments

From Dev

send arguments for function arguments in python

From Dev

Function arguments referring to the same variable

From Dev

Javascript function declaration with same arguments

From Dev

How to use multiprocessing to parallelize two calls to the same function, with different arguments, in a for loop?

Related Related

  1. 1

    Different arguments, but behavior of function is same

  2. 2

    Generate python function with different arguments

  3. 3

    Jest mock the same function twice with different arguments

  4. 4

    Function returns different answers with same arguments

  5. 5

    Ruby threads calling the same function with different arguments

  6. 6

    How to parallelize the same function using different arguments?

  7. 7

    Two different arguments for the same function but return different results?

  8. 8

    Run a function in parallel with different arguments - python

  9. 9

    partial function overload in python

  10. 10

    Apply the same function multiple time with different sets of arguments

  11. 11

    CMAKE: repeated calls to function with different arguments give the same results

  12. 12

    Different arguments with same template

  13. 13

    How to overload function switching by argument functor arguments

  14. 14

    "No overload for function "instantiate" takes 4 arguments"

  15. 15

    Initialize two Python classes with same arguments, get different results

  16. 16

    Recursive function with same arguments

  17. 17

    Combine same function with different parameters - Python

  18. 18

    Same python function giving different output

  19. 19

    Python 3 - Multiprocessing same function with different args

  20. 20

    Best way to "overload" function in python?

  21. 21

    Can I overload REST url resource having different arguments?

  22. 22

    Same function with same signatures in the same class/struct? Overload?

  23. 23

    Python Using Keyword and variable number of arguments in same function

  24. 24

    Use same arguments with different command

  25. 25

    functions with different names but same arguments

  26. 26

    send arguments for function arguments in python

  27. 27

    Function arguments referring to the same variable

  28. 28

    Javascript function declaration with same arguments

  29. 29

    How to use multiprocessing to parallelize two calls to the same function, with different arguments, in a for loop?

HotTag

Archive