4

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.

8
  • 2
    Have words, phrases, and feature parameters and give them default values of None then check to see what got passed before processing.? Commented Mar 21, 2018 at 20:28
  • or use the *args, **kwargs construct to allow accepting arbitrary arguments. Commented Mar 21, 2018 at 20:30
  • 2
    Why do you want to overload the name? The two methods have distinct functionality, with no real conceptual overlap in either data processing or linguistics. Commented Mar 21, 2018 at 20:35
  • 1
    I don't see a duplication problem; I just see a naming problem. Commented Mar 21, 2018 at 20:35
  • 1
    Method overloading would make sense, if the data you are giving could have different representations, but the outcome is the same. I don't see why you would want to use method overloading. Commented Mar 21, 2018 at 20:36

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

What about: set_features(phrases=phrases)? I think SortedSet accepts None, but set doesn't. That's assuming it makes sense to allow None at all...
1

Python allows default arguments.

def set_features(self, features=None, words=None, phrases=None):
    if features is not None:
        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)

you could then call it with set_features(features=features) or set_features(words=words, phrases=phrases)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.