super

Python3, calling super's __init__ from a custom exception

本小妞迷上赌 提交于 2021-02-11 14:17:12
问题 I have created custom exception in python 3 and the over all code works just fine. But there is one thing I am not able to wrap my head around is that why do I need to send my message to the Exception class's __init__() and how does it convert the Custom exception into that string message when I try to print the exception since the code in the Exception or even the BaseException does not do much. Not quite able to understand why call the super().__init__() from custom exception? 回答1: This is

In Java super.getClass() prints an unexpected name in the derived class - why is that? [duplicate]

谁说我不能喝 提交于 2021-02-08 10:08:02
问题 This question already has answers here : In Java super.getClass() prints “Child” not “Parent” - why is that? (3 answers) Closed 3 years ago . Could you help me understand what gets printed in the code below System.out.println(super.getClass().getName()); ? I see "PrintSubClass3" printed, even when I have mentioned super . class PrintClass { int x = 0; int y = 1; void printMe() { System.out.println("X is " + x + ", Y is " + y); System.out.println("I am an instance of the class " +super

In Java super.getClass() prints an unexpected name in the derived class - why is that? [duplicate]

拟墨画扇 提交于 2021-02-08 10:06:59
问题 This question already has answers here : In Java super.getClass() prints “Child” not “Parent” - why is that? (3 answers) Closed 3 years ago . Could you help me understand what gets printed in the code below System.out.println(super.getClass().getName()); ? I see "PrintSubClass3" printed, even when I have mentioned super . class PrintClass { int x = 0; int y = 1; void printMe() { System.out.println("X is " + x + ", Y is " + y); System.out.println("I am an instance of the class " +super

What does it mean when a parent class has a super()?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 08:26:11
问题 As far as i know, super() is used to call the methods overwritten in the subclass (like an overwritten __init__ in the subclass). Also, i know that all python classes inherit from a special class called object . While studying OOP i ran into a weird example: class A(object): def __init__(self): print('A.__init__') super().__init__() class B(object): def __init__(self): print('B.__init__') super().__init__() class C(A, B): def __init__(self): print('C.__init__') super().__init__() I understand

Java: How to call super method from inner in-place class

萝らか妹 提交于 2021-02-04 16:52:06
问题 I have base class Foo with method spam and class Bar which overrides spam . I need to call spam of base class in method of some callback object which is defined in-place: public class Foo { public void spam() { // ... } } public class Bar extends Foo { @Override public void spam() { objectWhichRequireCallback(new Callback { @Override public void onCallback() { super.spam(); } }); } } This code is not working because super is related to Callback , not Bar class. Is it possible to call super

Is calling super's constructor redundant in this case? [duplicate]

廉价感情. 提交于 2021-01-29 18:14:27
问题 This question already has answers here : Is it unnecessary to put super() in constructor? (6 answers) Closed 6 years ago . I always thought that when creating an object with a sub-class, we need to explicitly use super(arguments list) to call the constructor of the super class. However I did an experiment and realize that even without using the super() , the super class's constructor will be called automatically. Is this true? If this is true, when is super() redundant and when it is not?

Overriding the serialize_field() method in Scrapy

Deadly 提交于 2021-01-29 10:50:19
问题 Im using code from Scrapy documentation, with "Product" class item created from scrapy.exporter import XmlItemExporter class ProductXmlExporter(XmlItemExporter): def serialize_field(self, field, name, value): if field == 'price': return f'$ {str(value)}' return super(Product, self).serialize_field(field, name, value) and always get error from command line return super(Product, self).serialize_field(field, name, value) TypeError: super(Product, obj): obj must be an instance or subtype of type

super() usage in multiple inheritance in python

允我心安 提交于 2021-01-28 08:17:57
问题 I am new to python. I am trying to understand super() functionality in python multiple inheritance. class B(): def __init__(self): print("__init__ of B called") self.b = "B" class C(): def __init__(self): print("__init__ of C called") self.c = "C" class D(B, C): def __init__(self): print("__init__ of D called") super().__init__() def output(self): print(self.b, self.c) d = D() d.output() I am getting the following error: AttributeError: 'D' object has no attribute 'c' 回答1: super() will find

When do you need to pass arguments to python super()?

走远了吗. 提交于 2021-01-28 02:03:09
问题 A use case of the super() builtin in python is to call an overridden method. Here is a simple example of using super() to call Parent class's echo function: class Parent(): def echo(self): print("in Parent") class Child(Parent): def echo(self): super().echo() print("in Child") I've seen code that passes 2 parameters to super() . In that case, the signature looks somehing like super(subClass, instance) where subClass is the sub class calling super() from, and instance is the instance the call

Python 3 super and metaprogramming

北城以北 提交于 2021-01-27 16:16:44
问题 I'm trying to duplicate and then modify a class programmatically but I'm running into problems with python 3's magic super for example the following class Base(): def __init__(self): print("Base init") class Orginal(Base): def __init__(self): super().__init__() print("Orginal init") Modified = type(Orginal.__name__, Orginal.__bases__, dict(Orginal.__dict__)) Modified.f = lambda self: print("f") m = Modified() raises TypeError: super(type, obj): obj must be an instance or subtype of type So I