super

Calling super class method in multiple inheritance

时光总嘲笑我的痴心妄想 提交于 2019-12-12 04:58:08
问题 I have the following code: class A: pass class B(A): def foo(self, a): if a: return 'B' return super(B, self).foo(a) class C: def foo(self, a): return 'C' class D(B, C): def foo(self, a): return super().foo(a) d = D() print(d.foo(0)) When I call d.foo(0) based on MRO it first calls the foo method of B class and inside that, if the condition is wrong and it will return super(B, self).foo(0) but class A has no foo method and I expect this error: AttributeError: 'super' object has no attribute

Inheritance and Super in JavaScript

半腔热情 提交于 2019-12-12 03:54:21
问题 I am on Day 3 of learning JavaScript. I cames across this code: class B { constructor(name) { this.name = name; } printn() { return this.name; } } class A extends B { constructor(name, age) { super(name); this._age = age; } get age() { return this._age; } printName(){ return super.printn(); } } let c = new A("Testing", "37"); Console.log(c.printn()); Can anybody explain what does this code do. What is the constructor and super() keyword. I believe it is for inheritance? I did Google to find

Python: nested class with static method fails

孤街醉人 提交于 2019-12-12 01:02:28
问题 What is wrong with the following code? class A: def A_M(self): pass class B: @staticmethod def C(): super(B).A_M() error (Python 2.7.3): >>> a = A() >>> a.B.C() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "..x.py", line 36, in C def C(): super(B).A_M() NameError: global name 'B' is not defined Edit : the solution was simple as this: class A: def A_M(self): pass class B: @staticmethod def C(): A().A_M() #use of A() instead of supper, etc. Important Note that

Android - which Activity methods require a super constructor?

风格不统一 提交于 2019-12-11 19:05:12
问题 In most Android apps, onCreate() is overridden with the first bit of code being super.onCreate(savedInstanceState) and I know this gathers the savedInstanceState Bundle and is neccessary for compilation but what about all of the constructors? onResume() , onStop() , onStop() etc. Which overridden methods have important super constructors you need to include? Is there a list somewhere? I see the super constructors included in some code, not in others.. I have included them sometimes myself,

Java: Catch exception from super constructor

怎甘沉沦 提交于 2019-12-11 18:12:35
问题 I try to write my own loader-class which loads an encryted class. Therefore I also override the contruction loader(ClassLoader paramClassLoader, File paramFile) , which calls super(new URL[] { paramFile.toURI().toURL() }, paramClassLoader); . The call ".toUrl()" can throw an MalformedURLException , so compiling the following code ... public class loader extends URLClassLoader { public static void main(String[] args)throws Exception{ Object localObject = new loader(loader.class.getClassLoader(

Python super: base class method calls another method

这一生的挚爱 提交于 2019-12-11 11:35:04
问题 I came across this unexpected behavior using Python super , so I thought I would ask. I am aware of basic super usage but would like someone to elaborate more on my problem here. Consider the code: class Base (object): def f1 (self): print 'Base f1' def f2 (self): print 'Base f2' self.f1() class Derived (Base): def f1 (self): print 'Derived f1' def f2 (self): print 'Derived f2' super(Derived, self).f2() The call to Derived().f2() results in: Derived f2 Base f2 Derived f1 I was rather

How do I call super in a method decorator in Python 3? [duplicate]

╄→гoц情女王★ 提交于 2019-12-11 09:19:49
问题 This question already has an answer here : Given a method, how do I return the class it belongs to in Python 3.3 onward? (1 answer) Closed 5 years ago . How do I fill in the ??? ? def ensure_finished(iterator): try: next(iterator) except StopIteration: return else: raise RuntimeError def derived_generator(method): def new_method(self, *args, **kwargs): x = method(self, *args, **kwargs) y = getattr(super(???, self), method.__name__)\ (*args, **kwargs) for a, b in zip(x, y): assert a is None

Java - Sorting out subclass from ArrayList of superclass

橙三吉。 提交于 2019-12-11 08:59:13
问题 I have an ArrayList which contains objects of the super class and some subclass objects. Let's call them subclass1 and subclass2. Is there a way I can go ArrayList and discern which objects are SuperClass, subclass1 and subclass2. So I can put them into ArrayList and ArrayList. This is an overly simplified version but it demonstrates what I'm hoping to do. public class food{ private String name; public food(String name){ this.name = name; } } public class pudding extends food{ public pudding

Specifying which object to get the super of

一世执手 提交于 2019-12-11 08:47:00
问题 Like the titles says, I want to specify the super of an NSArrayController , something along the lines of self = [super[NSArrayController] function] , but have had no luck searching for this. Any ideas? Thanks in advance. Edited to remove abstract examples as they're confusing people as to the nature of my question. The purpose of this is to programmatically do what a simple binding of 'add' from an NSArrayController to an NSButton would do in IB. There are several arrayControllers in my

Assuming `obj` has type `objtype`, are `super(cls,obj)` and `super(cls,objtype)` the same?

蓝咒 提交于 2019-12-11 05:29:32
问题 In Python, assuming that obj has type objtype , are super(cls,obj) and super(cls,objtype) the same? Is it correct that super(cls,obj) converts obj to another object whose class is a superclass of objtype which is after cls in the MRO of objtype ? What does super(cls,objtype) mean then? For example, given an implementation of the Singleton design pattern: class Singleton(object): _singletons = {} def __new__(cls, *args, **kwds): if cls not in cls._singletons: cls._singletons[cls] = super