super

Python super(Class, self).method vs super(Parent, self).method

本秂侑毒 提交于 2019-11-30 14:20:04
This question is derive from the following question , let's say class B extends class A class A(object): def do_work(self): print 123 class B(A): def do_work(self): super(B,self).do_work() # versus the next statement super(A,self).do_work() # what's the difference? super(B,self).do_work() will call the do_work function as seen by the parent class of B - that is, A.do_work . super(A,self).do_work() will call the do_work function as seen by the parent class of A - that is, object.do_work (which probably doesn't exist, and thus would likely raise an exception). 来源: https://stackoverflow.com

Any way to _not_ call superclass constructor in Java?

天涯浪子 提交于 2019-11-30 13:39:52
问题 If I have a class: class A { public A() { } } and another class B extends A { public B() { } } is there any way to get B.B() not to call A.A() ? 回答1: There is absolutely no way to do this in Java; it would break the language specification. JLS 12 Execution / 12.5 Creation of New Class Instances Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure: Assign the arguments

super confusing python multiple inheritance super()

 ̄綄美尐妖づ 提交于 2019-11-30 13:05:32
I was playing around with the multiple inheritance in python and I come a cross a situation that I can't understand how it happen. Here is the inheritance layout: A F / \ | B C | \ | / \ | / D The ABCD diamond that everyone familiar with. Plus an extra class "F" I throw it in for fun. Here is the code: class A(object): def foo(self, call_from): print "foo from A, call from %s" % call_from super(A, self).foo("A") class B(A): def foo(self, call_from): print "foo from B, call from %s" % call_from super(B, self).foo("B") class C(A): def foo(self, call_from): print "foo from C, call from %s" % call

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

我怕爱的太早我们不能终老 提交于 2019-11-30 12:43:27
问题 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 why - what exactly is it that prevented super being implemented in a way that would allow the operator access? Subscriptable was just an example, I have the same question for __getattr__ , __init__ , etc. The docs attempt to

super respondsToSelector: returns true but actually calling super (selector) gives “unrecognized selector sent to instance”

不问归期 提交于 2019-11-30 11:06:09
OK, I am a little confused. I have a subclass of UIScrollView, which is my attempt at a horizontally scrolling "table view" like UI element. UIScrollView itself sets up UIGestureRecognizers it uses internally, and it appears to set itself up as the delegate for those UIGestureRecognizers. I also have my own UIGestureRecognizer setup on my horizontal table elements/cells and my own class set as delegate for my own UIGestureRecognizer. Since my class is a subclass of UIScrollView, at runtime, the UIGestureRecognizer delegate calls come to my class for both the UIScrollView in-built

How can I use super() with one argument in python

瘦欲@ 提交于 2019-11-30 11:04:21
While reading about the super() object in Python, I read the following statement: If the second argument is omitted, the super object returned is unbound What does this exactly mean and how do I use super() with one argument in code? Python function objects are descriptors , and Python uses the descriptor protocol to bind functions to an instance. This process produces a bound method . Binding is what makes the 'magic' self argument appear when you call a method, and what makes a property object automatically call methods when you try to use the property as an attribute on instances. super()

python multiple inheritance passing arguments to constructors using super

旧街凉风 提交于 2019-11-30 10:54:50
问题 Consider the following snippet of python code class A(object): def __init__(self, a): self.a = a class B(A): def __init__(self, a, b): super(B, self).__init__(a) self.b = b class C(A): def __init__(self, a, c): super(C, self).__init__(a) self.c = c class D(B, C): def __init__(self, a, b, c, d): #super(D,self).__init__(a, b, c) ??? self.d = d I am wondering how can I pass a , b and c to corresponding base classes' constructors. 回答1: Well, when dealing with multiple inheritance in general, your

Is the current Ruby method called via super?

孤者浪人 提交于 2019-11-30 05:39:48
问题 Within a method at runtime, is there a way to know if that method has been called via super in a subclass? E.g. module SuperDetector def via_super? # what goes here? end end class Foo include SuperDetector def bar via_super? ? 'super!' : 'nothing special' end end class Fu < Foo def bar super end end Foo.new.bar # => "nothing special" Fu.new.bar # => "super!" How could I write via_super? , or, if necessary, via_super?(:bar) ? 回答1: The ultimate mix between my other, @mudasobwa's and @sawa's

Which Android Fragment lifecycle methods require super

倾然丶 夕夏残阳落幕 提交于 2019-11-30 03:41:24
问题 Currently (Android API 17), the only mention of super in the Android Reference on Fragment is casually via some code examples (unlike the Android Reference on Activity, which carefully notes where super is required). SO suggests searching the web as needed, or waiting for a crash, to identify where a call to super is required. I'm asking SO users to share their knowledge on which of the Fragment lifecycle methods require a call to super . Fragment lifecycle methods - require call to super

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

爷,独闯天下 提交于 2019-11-30 02:03:17
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) | super(type, type2) -> bound super object; requires issubclass(type2, type) I understand that in Python3.x it's now possible to juse use super() within a class definition, but I don't understand why super(obj) is not possible. Or super(self) within a class definition. I know there must be a reason for it, but I