2

I am trying to subclass from sklearn.svm.LinearSVC and noticed the * argument in the signature. I'm not sure if this * refers to **kwargs or *args or something else. I am trying subclass the init function as follows. In this scenario I'm have added a single additional argument new_string_in_subclass the init function. from sklearn.svm import LinearSVC

class LinearSVCSub(LinearSVC):
    def __init__(self, penalty='l2', loss='squared_hinge', *, dual=True, tol=0.0001, C=1.0, multi_class='ovr',
                 fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=None,
                 max_iter=1000, sampler: new_string_in_subclass=None):

        super(LinearSVCSub, self).__init__(penalty=penalty, loss=loss, *, dual=dual, tol=tol,
                                            C=C, multi_class=multi_class, fit_intercept=fit_intercept,
                                                  intercept_scaling=intercept_scaling, class_weight=class_weight,
                                                  verbose=verbose, random_state=random_state, max_iter=max_iter)

        self.new_string_in_subclass = new_string_in_subclass

If I want to maintain the functionality of the LinearSVC class's other methods, do I need to pass the * argument to the super class's __init__ function? If so how do I do this? Right now I get a SyntaxError as below:

super(LinearSVCSub, self).init(penalty=penalty, loss=loss, *, dual=dual, tol=tol, ^ SyntaxError: invalid syntax

Hawklaz
  • 306
  • 4
  • 20
  • 2
    This `*` syntax is for keyword-only arguments. It was added in Python 3.8. See [Positional-only parameters](https://docs.python.org/3/whatsnew/3.8.html#positional-only-parameters) in the release notes. – Pascal Getreuer Oct 19 '20 at 01:36

2 Answers2

4

A * only goes in the definition of a method, not in calls to that method. The * indicates that all parameters after it can only be supplied by name, not by position. Just leave it out when calling the super, and you should be fine.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • I've posted [another question](https://stackoverflow.com/q/64430351/11542679) about another problem I ran into after this was sorted out. Appreciate if you could have a look. It's about using the above setup with sklearn `GridSearchCV ` – Hawklaz Oct 19 '20 at 15:25
1

The asterisk serves to separate keyword-only parameters: Bare asterisk in function arguments? You should skip it when instantiating the sklearn class.

Ben Reiniger
  • 10,517
  • 3
  • 16
  • 29