1

I have a code as follows:

class A(): def something(): #Do something class B(A): def something(): super(B, self).something() #Do something class C(B): def something(): if VeryRareSpecificCase: super(super(C,self)).something() else: super(C, self).something() #Do something 

However, it raises the error TypeError: must be type, not super.

1
  • 1
    What did you expect super(super(C, self)) to refer to? If the answer is super(B, self), then you aren't using super correctly; you should just be hardcoding the class references. Commented Sep 7, 2017 at 3:02

1 Answer 1

1

Provided you aren't using a complicated diamond inheritance, you can skip a parent in the chain with this approach:

class C(B): def something(self): if VeryRareSpecificCase: super(B, self).something() # <-- note: B passed explicitly! else: super().something() 

If you really have just C --> B --> A --> object, as shown in your MCVE, this should be safe. However, if you have something more complicated than that, you'll have to work with the mro directly and specify exactly what behaviour you need manually.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm importing B from another module before inheriting it and A is in a module I don't want to access. Would this affect anything or can I still do this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.