super

Need to mock out some base class behavior in a python test case

孤街浪徒 提交于 2019-12-11 05:17:55
问题 My title is fairly descriptive, but here goes. Suppose I have this setup. class BaseClass(object): def __init__(self): pass def base_function(self, param="Hello World"): print param #multiple inheritance probably irrelevant but my problem deals with it class DerivedClass(BaseClass, AnotherBaseClass): def __init__(self): pass def advanced_function(self): #blah blah blah #code code code self.base_function() Now, I have a situation where I am testing a derived class, but in doing so, I need to

C++ cannot find type defined in template base class which inherits from the current template class

*爱你&永不变心* 提交于 2019-12-11 00:47:04
问题 I'm trying to write a variation of the template class defining a super type idiom. The class Inherit introduces the type Super to denote the possibly very long super type, and also needs to know the derived type New to do some extra things which I'm not showing here. This works fine if the type passed to New is not a template, but fails with templates. Here is a full example compiled with clang++-3.8 -std=c++1y -Wall (gcc gives the same output): struct SomeBase {}; template<class New, class

How can I use Python's super() to update a parent value?

人走茶凉 提交于 2019-12-10 21:39:30
问题 I'm new to inheritance and all of the previous discussions about inheritance and Python's super() function are a bit over my head. I currently use the following code to update a parent object's value. #!/usr/bin/env python # test.py class Master(object): mydata = [] def __init__(self): s1 = Sub1(self) s2 = Sub2(self) class Sub1(object): def __init__(self,p): self.p = p self.p.mydata.append(1) class Sub2(object): def __init__(self,p): self.p = p self.p.mydata.append(2) if __name__ == "__main__

Jest unit-testing if super() is called

一个人想着一个人 提交于 2019-12-10 15:46:06
问题 I have a custom error class that extends the built-in Error class in Javascript. The problem I came up with is that "super()" method is not checked if it is called or not through my Jest unit testing. export class AppError extends Error { public name: string; public message: string; public status?: number; public data?: any; constructor(message: string, status?: number, data?: any) { super(); <-- this guy!! this.name = 'AppError'; this.status = status || 500; this.message = message; this.data

Keep reference to new object passed into super constructor

微笑、不失礼 提交于 2019-12-10 13:03:11
问题 Is there a good way to maintain a reference to an object created in a constructor that needs to be passed to the super constructor of some extended class (other than making it accessible from the super class or passing it into the constructor as a parameter)? Let me clarify with an example. Take this class (which I can't modify and which doesn't give me access to foo): class TestA { TestA(Object foo) {} } Now, I would like to extends this class as follows: class TestB extends TestA { Object

Regarding python MRO and how super() behaves

大兔子大兔子 提交于 2019-12-10 11:54:05
问题 Today i was trying to figure out how __mro__ and super works in python, I found something interesting as well as strange to me, because I got something which is not that i understood after reading __mro__ . Here is the code snippets. Code Snippets 1: #!/usr/bin/pyhon class A(object): def test(self): return 'A' class B(A): def test(self): return 'B to %s' % super(B, self).test() class C(A): def test(self): return 'C' class D(B, C): pass print D().test() Output : B to C Code snippet 2: When I

Why is 'super' a keyword rather than a method in Ruby?

偶尔善良 提交于 2019-12-10 03:14:31
问题 In Ruby, super is a keyword rather than a method. Why was it designed this way? Ruby's design tends toward implementing as much as possible as methods; keywords are usually reserved for language features that have their own grammar rules. super , however, looks and acts like a method call. (I know it would be cumbersome to implement super in pure Ruby, since it would have to parse the method name out of caller , or use a trace_func. This alone wouldn't prevent it from being a method, because

关于Scala多重继承的菱形问题

自古美人都是妖i 提交于 2019-12-09 21:04:23
在Scala中的trait中引入了混入的概念,即Mixin of trait。 什么是混入(mixin) 可能翻译不准确,有人也称之为混入类(mixins),混入是一种组合的抽象类,主要用于多继承上下文中为一个类添加多个服务,多重继承将多个 mixin 组合成一个类。例如,如果你有一个类表示“马”,你可以实例化这个类来创建一个“马”的实例,然后通过继承像“车库”和“花园”来扩展它,使用 Scala 的写法就是: val myHouse = new House with Garage with Garden 从 mixin 继承并不是一个特定的规范,这只是用来将各种功能添加到已有类的方法。在 OOP 中,有了mixin,你就有通过它来提升类的可读性。 object Test { def main(args: Array[String]): unit = { class Iter extends StringIterator(args(0)) with RichIterator[char] val iter = new Iter iter foreach System.out.println } } 如Iter类通过RichIterator和StringIterator这两个父类混入构成,第一个父类仍然称为超类(superclass),第二个父类则称为混入类(mixin)。

What happens if you call an overridden method using super in a constructor

白昼怎懂夜的黑 提交于 2019-12-09 16:58:07
问题 There is two classes Super1 and Sub1 Super1.class public class Super1 { Super1 (){ this.printThree(); } public void printThree(){ System.out.println("Print Three"); } } Sub1.class public class Sub1 extends Super1 { Sub1 (){ super.printThree(); } int three=(int) Math.PI; public void printThree(){ System.out.println(three); } public static void main(String ...a){ new Sub1().printThree(); } } When I invoke the method printThree of class Sub1 I expected the output to be: Print Three 3 Because

When to call Python's super().__init__()?

半腔热情 提交于 2019-12-08 18:38:07
问题 (I am unable to find a reference anywhere on this matter after some Googling.) The scenario can be clearly demonstrated with this short code sample: class X: def __init__(self, stuff): self.__stuff = stuff class Y(X): def __init__(self, stuff): # Is it safe to execute statements before calling super.__init__()? new_stuff = self.call_another_method(stuff) super(Y, self).__init__(new_stuff) Using CPython 3.x, the above code sample works -- assuming call_another_method() exists. It this coding