superclass

super() raises “TypeError: must be type, not classobj” for new-style class

前提是你 提交于 2019-11-26 02:39:49
问题 The following use of super() raises a TypeError: why? >>> from HTMLParser import HTMLParser >>> class TextParser(HTMLParser): ... def __init__(self): ... super(TextParser, self).__init__() ... self.all_data = [] ... >>> TextParser() (...) TypeError: must be type, not classobj There is a similar question on StackOverflow: Python super() raises TypeError, where the error is explained by the fact that the user class is not a new-style class. However, the class above is a new-style class, as it

why java polymorphism not work in my example

谁都会走 提交于 2019-11-26 02:25:07
问题 I have these 4 java clases: 1 public class Rect { double width; double height; String color; public Rect( ) { width=0; height=0; color=\"transparent\"; } public Rect( double w,double h) { width=w; height=h; color=\"transparent\"; } double area() { return width*height; } } 2 public class PRect extends Rect{ double depth; public PRect(double w, double h ,double d) { width=w; height=h; depth=d; } double area() { return width*height*depth; } } 3 public class CRect extends Rect{ String color;

How to invoke the super constructor in Python?

你说的曾经没有我的故事 提交于 2019-11-26 01:28:22
问题 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. 回答1: super() returns a parent-like object in new-style classes : class A(object): def __init__(self): print("world") class B(A): def __init__(self): print("hello") super(B, self).__init__() B() 回答2: In line with

Why is super.super.method(); not allowed in Java?

社会主义新天地 提交于 2019-11-25 21:50:57
问题 I read this question and thought that would easily be solved (not that it isn\'t solvable without) if one could write: @Override public String toString() { return super.super.toString(); } I\'m not sure if it is useful in many cases, but I wonder why it isn\'t and if something like this exists in other languages. What do you guys think? EDIT: To clarify: yes I know, that\'s impossible in Java and I don\'t really miss it. This is nothing I expected to work and was surprised getting a compiler