super

What's the difference between super() and Parent class name?

感情迁移 提交于 2019-11-28 23:40:42
Is there a difference between using super() and using the parent class name directly? For example: class Parent: def __init__(self): print("In parent") self.__a=10 class Child(Parent): def __init__(self): super().__init__() # using super() Parent.__init__(self) # using Parent class name c=Child() Is there internally a difference between super().__init__() and Parent.__init__(self) ? Willem Van Onsem Not in this case . But in general , and especially when you use multiple inheritance , super() delegates to the next object in the Method Resolution Order (MRO) as is specified in the documentation

Python 2.x super __init__ inheritance doesn't work when parent doesn't inherit from object

做~自己de王妃 提交于 2019-11-28 22:50:44
I have the following Python 2.7 code: class Frame: def __init__(self, image): self.image = image class Eye(Frame): def __init__(self, image): super(Eye, self).__init__() self.some_other_defined_stuff() I'm trying to extend the __init__() method so that when I instantiate an 'Eye' it does a bunch of other stuff (self.some_other_defined_stuff()), in addition to what Frame sets up. Frame.__init__() needs to run first. I get the following error: super(Eye, self).__init__() TypeError: must be type, not classobj Which I do not understand the logical cause of. Can someone explain please? I'm used to

Why can I not use “super” variable from a static context, even though “super” refers to the parent class and NOT a class instance, unlike “this”?

微笑、不失礼 提交于 2019-11-28 21:33:18
I'm talking java language. Variable "this", when used inside a class, refers to the current instance of that class, which means you cannot use "this" inside a static method. But "super", when used inside a class, refers to the superclass of that class, not an instance of the superclass, which should mean that you can use "super" inside a static method. But it turns out you cannot. A possible explanation would be to say that "super" also refers to an instance of the superclass, but I can't see why it should... Here is the section in the JLS about the super keyword: http://docs.oracle.com/javase

Getting the name of a sub-class from within a super-class

谁都会走 提交于 2019-11-28 20:59:37
Let's say I have a base class named Entity . In that class, I have a static method to retrieve the class name: class Entity { public static String getClass() { return Entity.class.getClass(); } } Now I have another class extend that. class User extends Entity { } I want to get the class name of User: System.out.println(User.getClass()); My goal is to see "com.packagename.User" output to the console, but instead I'm going to end up with "com.packagename.Entity" since the Entity class is being referenced directly from the static method. If this wasn't a static method, this could easily be solved

super() and @staticmethod interaction

余生长醉 提交于 2019-11-28 20:07:57
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 13, in <module> a = Second.getlist() File "asdf.py", line 9, in getlist l = super(Second).getlist() AttributeError: 'super' object has no attribute 'getlist' If I change the staticmethods to classmethods and pass the class instance to

What is a basic example of single inheritance using the super() keyword in Python?

◇◆丶佛笑我妖孽 提交于 2019-11-28 19:58:39
问题 Let's say I have the following classes set up: class Foo: def __init__(self, frob, frotz): self.frobnicate = frob self.frotz = frotz class Bar: def __init__(self, frob, frizzle): self.frobnicate = frob self.frotz = 34 self.frazzle = frizzle How can I (if I can at all) use super() in this context to eliminate the duplicate code? 回答1: In Python >=3.0, like this: class Foo(): def __init__(self, frob, frotz) self.frobnicate = frob self.frotz = frotz class Bar(Foo): def __init__(self, frob,

Using super with a class method

大城市里の小女人 提交于 2019-11-28 19:25:13
问题 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 = classmethod(do_something) TypeError: unbound method do_something() must be called with B instance as

React, why use super(props) inside of ES6 class constructor? [duplicate]

家住魔仙堡 提交于 2019-11-28 18:26:55
This question already has an answer here: What's the difference between “super()” and “super(props)” in React when using es6 classes? 10 answers I realize the super keyword can be used to call functions in a parent component. However, I'm not totally clear why you would use the super keyword in the example below - just passing it whatever props are being passed to the constructor. Can someone please shed some light on the various reasons for using the super keyword in an ES6 class constructor, in react? constructor(props) { super(props); this.state = { course: Object.assign({}, this.props

In Java super.getClass() prints “Child” not “Parent” - why is that?

邮差的信 提交于 2019-11-28 13:52:02
In Java classes and objects, we use "this" keyword to reference to the current object within the class. In some sense, I believe "this" actually returns the object of itself. Example for this: class Lion { public void Test() { System.out.println(this); //prints itself (A Lion object) } } In the scenario of a superclass and subclass. I thought that "super" keyword would return the object of the superclass. However it seems that I got it wrong this time: Example: class Parent { public Parent(){ } } class Child extends Parent { public Child(){ System.out.println(super.getClass()); //returns Child

super keyword without extends to the super class

梦想的初衷 提交于 2019-11-28 12:32:25
There is a simple program, in the constructor, super() is called without extends to the super class, I can not understand what will does this in this situation ? public class Student { private String name; private int rollNum; Student(String name,int rollNum){ super();// I can not understand why super keyword here. this.name=name; this.rollNum=rollNum; } public static void main(String[] args) { Student s1 = new Student("A",1); Student s2 = new Student("A",1); System.out.println(s1.equals(s2)); } } JB Nizet Every class that doesn't explicitly extend another class implicitly extends java.lang