Shamoon
Shamoon

Reputation: 43521

Can a subclass of Python class have different arguments than the Base Class?

I am using Python 3.6.7 and I have:

class CodeModel:
    def tokenize(self, lexer, save_tokens=None):
        tokens = np.array([], dtype='object')
        line_count = 0

Then I have:

class JSCode(CodeModel):
    def tokenize(self, **kwargs):
        lexer = JavascriptLexer()
        super().tokenize(lexer, **kwargs)

Within the CodeModel, I have:

self.tokenize(save_tokens='stuff')

I want it to then call the tokenize of the JSCode, which doesn't need save_tokens and pass that to the base class, CodeModel.tokenize.

However, the way I'm doing it doesn't seem to work. The error I get is:

    self.tokenize(save_tokens=save_tokens)
TypeError: tokenize() got an unexpected keyword argument 'save_tokens'

What am I doing wrong?

Upvotes: 0

Views: 124

Answers (1)

Maarten Fabré
Maarten Fabré

Reputation: 7058

If type(self) is CodeModel, but you know you want to call the JSCode.tokenize, you can do that with JSCode.tokenize(self)

I can think of no good reasons why self should be of type CodeModel then. If the code doesn't need any other features of JSCode, why have it under that class anyway. Just have a method in the module namespace

Upvotes: 1

Related Questions