superclass

How to invoke the super constructor in Python?

How to invoke the super constructor in Python? Question: class A: def __init__(self): print(“world”) class B(A): def __init__(self): print(“hello”) B() # output: hello In all other languages I’ve worked with the super constructor is invoked implicitly. How does one invoke it in Python? I would expect super(self) but this doesn’t work. Asked By: Mike || …

Total answers: 7

Checking if A is superclass of B in Python

Checking if A is superclass of B in Python Question: class p1(object): pass class p2(p1): pass So p2 is the subclass of p1. Is there a way to find out programmatically that p1 is [one of] the superclass[es] of p2 ? Asked By: Andz || Source Answers: I think you meant to use “class” instead …

Total answers: 4

Should __init__() call the parent class's __init__()?

Should __init__() call the parent class's __init__()? Question: I’m used that in Objective-C I’ve got this construct: – (void)init { if (self = [super init]) { // init class } return self; } Should Python also call the parent class’s implementation for __init__? class NewClass(SomeOtherClass): def __init__(self): SomeOtherClass.__init__(self) # init class Is this also true/false …

Total answers: 7

Inheritance and Overriding __init__ in python

Inheritance and Overriding __init__ in python Question: I was reading ‘Dive Into Python’ and in the chapter on classes it gives this example: class FileInfo(UserDict): “store file metadata” def __init__(self, filename=None): UserDict.__init__(self) self[“name”] = filename The author then says that if you want to override the __init__ method, you must explicitly call the parent __init__ …

Total answers: 5