super

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

super() raises "TypeError: must be type, not classobj" for new-style class Question: 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 …

Total answers: 7

Calling parent class __init__ with multiple inheritance, what's the right way?

Calling parent class __init__ with multiple inheritance, what's the right way? Question: Say I have a multiple inheritance scenario: class A(object): # code for A here class B(object): # code for B here class C(A, B): def __init__(self): # What’s the right code to write here to ensure # A.__init__ and B.__init__ get called? There’s …

Total answers: 11

Python: 'super' object has no attribute 'attribute_name'

Python: 'super' object has no attribute 'attribute_name' Question: I am trying to access a variable from the base class. Here’s the parent class: class Parent(object): def __init__(self, value): self.some_var = value And here’s the child class: class Child(Parent): def __init__(self, value): super(Child, self).__init__(value) def doSomething(self): parent_var = super(Child, self).some_var Now, if I try to run …

Total answers: 3

Is super() broken in Python-2.x?

Is super() broken in Python-2.x? Question: It’s often stated that super should be avoided in Python 2. I’ve found in my use of super in Python 2 that it never acts the way I expect unless I provide all arguments such as the example: super(ThisClass, self).some_func(*args, **kwargs) It seems to me this defeats the purpose …

Total answers: 5

Python super method and calling alternatives

Python super method and calling alternatives Question: I see everywhere examples that super-class methods should be called by: super(SuperClass, instance).method(args) Is there any disadvantage to doing: SuperClass.method(instance, args) Asked By: IanSR || Source Answers: There’s no penalty as-is, though your example is somewhat misguided. In the first example, it should be super(SubClass, instance).method(args) # Sub, …

Total answers: 2

How to avoid infinite recursion with super()?

How to avoid infinite recursion with super()? Question: I have code like this: class A(object): def __init__(self): self.a = 1 class B(A): def __init__(self): self.b = 2 super(self.__class__, self).__init__() class C(B): def __init__(self): self.c = 3 super(self.__class__, self).__init__() Instantiating B works as expected but instantiating C recursed infinitely and causes a stack overflow. How can …

Total answers: 1

Using super with a class method

Using super with a class method Question: I’m trying to learn the super() function in Python. I thought I had a grasp of it until I came over this example (2.6) and found myself stuck. http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html#super-with-classmethod-example Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “test.py”, line 9, in do_something do_something = …

Total answers: 5

How can I update an attribute created by a base class' mutable default argument, without modifying that argument?

How can I update an attribute created by a base class' mutable default argument, without modifying that argument? Question: I’ve found a strange issue with subclassing and dictionary updates in new-style classes: Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 >>> class a(object): … def __init__(self, props={}): … self.props …

Total answers: 3