super

what does super() do without any arguments?

流过昼夜 提交于 2019-11-30 01:28:49
I'm learning react from the docs , but not sure what the super() does in this example. Usually, doesn't it take the arguments that are passed to making a new instance and then calls React.Component's constructor method to incorporate these arguments into the instance? What does it do without any arguments? class LikeButton extends React.Component { constructor() { super(); this.state = { liked: false }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({liked: !this.state.liked}); } render() { const text = this.state.liked ? 'liked' : 'haven\'t liked'; return (

Require override of method to call super

流过昼夜 提交于 2019-11-30 00:10:21
问题 I want that when a child class overrides a method in a parent class, the super.method() is called in that child method. Is there any way to check this at compile time? If not, how would I go about throwing a runtime exception when this happens? 回答1: There's no way to require this directly. What you can do, however, is something like: public class MySuperclass { public final void myExposedInterface() { //do the things you always want to have happen here overridableInterface(); } protected void

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

北城余情 提交于 2019-11-29 22:59:14
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? JAB 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, frizzle) super().__init__(frob, 34) self.frazzle = frizzle Read more here: http://docs.python.org/3.1/library

Using super with a class method

試著忘記壹切 提交于 2019-11-29 22:46:14
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 first argument (got nothing instead) >>> It wasn't what I expected when I read this line right before

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

自古美人都是妖i 提交于 2019-11-29 21:52:05
问题 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? 回答1: 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

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

时间秒杀一切 提交于 2019-11-29 21:31:27
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) What's wrong with just doing it like this ?: class First(object): def __init__(self): print "first" class Second(object): def __init__(self): print "second" class Third(First, Second): def __init__(self): First.__init__(self) Second.__init__(self) print "that's it" On to super(), Andrew Kuchlings paper on Python Warts says: usage of super() will also be correct when

python, inheritance, super() method

江枫思渺然 提交于 2019-11-29 14:56:00
I'm new to python, I have the code below which I just can't get to work:- This is inheritance, I have a circle base class and I inherit this within a circle class (just single inheritance here). I understand the issue is within the ToString() function within the circle class, specifically the line, text = super(Point, self).ToString() +.. which requires at least a single argument, yet I get this: AttributeError: 'super' object has no attribute 'ToString' I know super has no ToString attribute, but the Point class does - My code: class Point(object): x = 0.0 y = 0.0 # point class constructor

ES6 What does super() actually do in constructor function?

左心房为你撑大大i 提交于 2019-11-29 10:43:44
! Hola, amigos. I have this little class inheritance structure class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } class ColorPoint extends Point { constructor(x, y, color) { super(x, y); this.color = color; } toString() { return super.toString() + ' in ' + this.color; } } let newObj = new ColorPoint(25, 8, 'green'); It compiles to this jsfiddle I get how it works in es6 in a silly way. But could somebody explain how does it work under the hood in es5. In a simpler form. Bergi super(…); is basically sugar for this = new

How do I force a polymorphic call to the super method?

拈花ヽ惹草 提交于 2019-11-29 03:43:53
I have an init method that is used and overridden through out an extensive heirarchy. Each init call however extends on the work that the previous did. So naturally, I would: @Override public void init() { super.init(); } And naturally this would ensure that everything is called and instantiated. What I'm wondering is: Can I create a way to ensure that the super method was called? If all of the init's are not call, there is a break down in the obejct, so I want to throw an exception or an error if somebody forgets to call super . TYFT ~Aedon Here's one way to raise an exception if a derived

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-29 01:21:12
问题 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