super

How can I redefine Fixnum's + (plus) method in Ruby and keep original + functionality?

血红的双手。 提交于 2019-11-27 16:38:18
问题 This throws me a SystemStackError in 1.9.2 Ruby ( but works in Rubinius ): class Fixnum def +(other) self + other * 2 end end but there is no super for + (based on other errors). How can I access the original + functionality? 回答1: Use alias_method . Alias Fixnum 's + to something else, then refer to it in the new + : class Fixnum alias_method :old_add, :+ def +(other) self.old_add(other) * 2 end end 回答2: Another interesting approach would be to pass a block to Fixnum's module_eval method. So,

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

一个人想着一个人 提交于 2019-11-27 14:38:56
问题 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

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

╄→гoц情女王★ 提交于 2019-11-27 11:35:05
问题 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)

How is super() in Python 3 implemented?

元气小坏坏 提交于 2019-11-27 09:14:57
I'm wondering how is the new super in Python 3 implemented. This question was born in my head after I have made a small example and I got a strange error. I'm using Pyutilib Component architecture (PCA) and I've made my custom metaclass to drive the creation of another class: from pyutilib.component.core import implements, SingletonPlugin, PluginMeta, Interface class IPass(Interface): pass class __MetaPlugin(PluginMeta): def __new__(cls, name, baseClasses, classdict): print(cls, name, baseClasses, classdict) if baseClasses: baseClasses += (SingletonPlugin,) return PluginMeta.__new__(cls, name,

Python's Multiple Inheritance: Picking which super() to call

大兔子大兔子 提交于 2019-11-27 07:23:53
In Python, how do I pick which Parent's method to call? Say I want to call the parent ASDF2's __init__ method. Seems like I have to specify ASDF1 in the super()..? And if I want to call ASDF3's __init__ , then I must specify ASDF2 ?! >>> class ASDF(ASDF1, ASDF2, ASDF3): def __init__(self): super(ASDF1, self).__init__() >>> ASDF() ASDF2's __init__ happened >>> class ASDF(ASDF1, ASDF2, ASDF3): def __init__(self): super(ASDF2, self).__init__() >>> ASDF() ASDF3's __init__ happened Seems bonkers to me. What am I doing wrong? Falmarri That's not what super() is for. Super basically picks one (or all

set attribute with javascript super method [duplicate]

北战南征 提交于 2019-11-27 07:08:28
问题 Possible Duplicate: Why are my JS object properties being overwritten by other instances Why isn't the attribute "t" changed after setT was called? I would expect "4" as output, but it prints "default". function Car(i) { var id = i; var t = "default"; this.getT = function() { return t; } this.setT = function(p) { t = p; // attribute t isn't changed .. } } function ECar(id) { Car.call(this, id); // super constructor call this.setT = function(p) { // override ECar.prototype.setT.call(this, p);

super keyword without extends to the super class

故事扮演 提交于 2019-11-27 07:00:59
问题 There is a code of simple program. In 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

Python 2.7 super() error [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-11-27 06:21:40
问题 This question already has answers here : super() fails with error: TypeError “argument 1 must be type, not classobj” when parent does not inherit from object (4 answers) Closed last year . Trying to create Tkinter window using super(). I get this error: super(Application, self)._ init _(master) TypeError: must be type, not classobj Code: import Tkinter as tk class Application(tk.Frame): def __init__(self, master): super(Application, self).__init__(master) self.grid() def main(): root = tk.Tk(

Emulate super in javascript

我怕爱的太早我们不能终老 提交于 2019-11-27 05:39:40
问题 Basically is there a good elegant mechanism to emulate super with syntax that is as simple as one of the following this.$super.prop() this.$super.prop.apply(this, arguments); Criteria to uphold are : this.$super must be a reference to the prototype. i.e. if I change the super prototype at run-time this change will be reflected. This basically means it the parent has a new property then this should be shown at run-time on all children through super just like a hard coded reference to the

super keyword unexpected here

安稳与你 提交于 2019-11-27 04:06:58
问题 According to ES6 shorthand initialiser, following 2 methods are same: In ES5 var person = { name: "Person", greet: function() { return "Hello " + this.name; } }; In ES6 var person = { name: "Person", greet() { return "Hello " + this.name; } }; Do the ES6 way is in anyway different from the previous way? If not then using "super" inside them should be also treated as equal, which doesn't hold true, please see below two variaiton: Below works let person = { greet(){ super.greet(); } }; Object