super

super() and @staticmethod interaction

super() and @staticmethod interaction Question: Is super() not meant to be used with staticmethods? When I try something like class First(object): @staticmethod def getlist(): return [‘first’] class Second(First): @staticmethod def getlist(): l = super(Second).getlist() l.append(‘second’) return l a = Second.getlist() print a I get the following error Traceback (most recent call last): File “asdf.py”, line …

Total answers: 4

Why do we have to use the __dunder__ methods instead of operators when calling via super?

Why do we have to use the __dunder__ methods instead of operators when calling via super? Question: Why do we have to use __getitem__ rather than the usual operator access? class MyDict(dict): def __getitem__(self, key): return super()[key] We get TypeError: ‘super’ object is not subscriptable. Instead we must use super().__getitem__(key), but I never fully understood …

Total answers: 3

Why is Python 3.x's super() magic?

Why is Python 3.x's super() magic? Question: In Python 3.x, super() can be called without arguments: class A(object): def x(self): print(“Hey now”) class B(A): def x(self): super().x() >>> B().x() Hey now In order to make this work, some compile-time magic is performed, one consequence of which is that the following code (which rebinds super to …

Total answers: 1

When calling super() in a derived class, can I pass in self.__class__?

When calling super() in a derived class, can I pass in self.__class__? Question: I’ve recently discovered (via StackOverflow) that to call a method in a base class I should call: super([[derived class]], self).[[base class method]]() That’s fine, it works. However, I find myself often copying and pasting between classes when I make a change and …

Total answers: 2

Python super() arguments: why not super(obj)?

Python super() arguments: why not super(obj)? Question: I am trying to understand when and how to use super() in Python correctly (either 2.7.x or 3.x) on >>> help(super) the interpreter tells me how to call it: class super(object) | super(type) -> unbound super object | super(type, obj) -> bound super object; requires isinstance(obj, type) | …

Total answers: 2

How does multiple inheritance work with the super() and different __init__() arguments?

How does multiple inheritance work with the super() and different __init__() arguments? Question: I’m just diving into some more advanced python subjects (well, advanced to me at least). I am now reading about multiple inheritance and how you can use super(). I more or less understand the way the super function is used, but (1) …

Total answers: 2

Python's Multiple Inheritance: Picking which super() to call

Python's Multiple Inheritance: Picking which super() to call Question: In Python, how do I pick which Parent’s method to call? Say I want to call the parent ASDF2’s __init__ method. Seems like I have to specify ASDF1 in the super()..? And if I want to call ASDF3’s __init__, then I must specify ASDF2?! >>> class …

Total answers: 3

Python, Overriding an inherited class method

Python, Overriding an inherited class method Question: I have two classes, Field and Background. They look a little bit like this: class Field( object ): def __init__( self, a, b ): self.a = a self.b = b self.field = self.buildField() def buildField( self ): field = [0,0,0] return field class Background( Field ): def __init__( …

Total answers: 5

Python super and setting parent class property

Python super and setting parent class property Question: I’m having a really strange problem with Python super() and inheritance and properties. First, the code: #!/usr/bin/env python3 import pyglet import pygame class Sprite(pyglet.sprite.Sprite): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.rect = pygame.Rect(0, 0, self.width, self.height) self.rect.center = self.x, self.y @property def x(self): return super().x @x.setter def …

Total answers: 2

Python super() behavior not dependable

Python super() behavior not dependable Question: For some reason, the super() method is not always behaving as expected, opting to return: TypeError(‘super(type, obj): obj must be an instance or subtype of type)’ I understand what the error means. I do not understand why it is coming up as an error. Here’s the snippet of code …

Total answers: 5