super

How to mock super reference (on super class)?

我们两清 提交于 2019-12-21 05:21:11
问题 Sometimes when I write unit tests I should to mock reference to superclass. I have read this question: question This answer answer with DI advice to refactor code. But I cannot it this answer another answer is not suitable if superclass method is enough big. In my case I have very big code. Yes I know that it is brokes SOLID OOD principes but I just should to write test. I have not enough time for refactor. said question was asked 4 years ago! Does currently Mockito or Powermock can resolve

How to mock super reference (on super class)?

蓝咒 提交于 2019-12-21 05:21:05
问题 Sometimes when I write unit tests I should to mock reference to superclass. I have read this question: question This answer answer with DI advice to refactor code. But I cannot it this answer another answer is not suitable if superclass method is enough big. In my case I have very big code. Yes I know that it is brokes SOLID OOD principes but I just should to write test. I have not enough time for refactor. said question was asked 4 years ago! Does currently Mockito or Powermock can resolve

What does it mean by the 'super object returned is unbound' in python?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 04:11:06
问题 According to http://docs.python.org/2/library/functions.html#super, If the second argument is omitted, the super object returned is unbound. Which is super(type). I am wondering what is unbounded and when is it bounded. 回答1: Edit: in the context of super , much of below is wrong. See the comment by John Y. super(Foo, a).bar returns the method called bar from the next object in the method resolution order (the MRO), in this case bound to the object a , an instance of Foo . If a was left out,

Second parameter of super()?

独自空忆成欢 提交于 2019-12-20 12:38:24
问题 A colleague of mine wrote code analogous to the following today, asked me to have a look, and it took me a while to spot the mistake: class A(): def __init__(self): print('A') class B(A): def __init__(self): super(B).__init__() b = B() The problem here is that there's no self parameter to super() in B 's constructor. What surprised me is that absolutely nothing happens in this case, i.e. no error, nothing. What does the super object created by super(B) contain? As an object, it clearly has a

TypeError: Super does not take Key word arguments?

假装没事ソ 提交于 2019-12-20 11:55:16
问题 First, here is my code: class Enemy(): def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage def is_alive(self): """Checks if alive""" return self.hp > 0 class WildBoar(Enemy): def __init__(self): super(WildBoar, name="Wild Boar", hp=10, damage=2).__init__() class Marauder(Enemy): def __init__(self): super(Marauder, name="Marauder", hp=20, damage=5).__init__() class Kidnappers(Enemy): def __init__(self): super(Kidnappers, name="The Kidnappers", hp=30, damage

Python, Overriding an inherited class method

帅比萌擦擦* 提交于 2019-12-20 08:08:57
问题 I have two classes, Field and Background . They look a little bit like this: class Field( object ): def __init__( self, a, b ): self.a = a self.b = b self.field = self.buildField() def buildField( self ): field = [0,0,0] return field class Background( Field ): def __init__( self, a, b, c ): super(Background, self).__init__( a, b ) self.field = self.buildField( c ) def buildField( self, c ): field = [c] return field a, b, c = 0, 1, 2 background = Background( a, b, c ) This error is pointing to

Python, Overriding an inherited class method

心不动则不痛 提交于 2019-12-20 08:08:05
问题 I have two classes, Field and Background . They look a little bit like this: class Field( object ): def __init__( self, a, b ): self.a = a self.b = b self.field = self.buildField() def buildField( self ): field = [0,0,0] return field class Background( Field ): def __init__( self, a, b, c ): super(Background, self).__init__( a, b ) self.field = self.buildField( c ) def buildField( self, c ): field = [c] return field a, b, c = 0, 1, 2 background = Background( a, b, c ) This error is pointing to

super() and changing the signature of cooperative methods

女生的网名这么多〃 提交于 2019-12-20 06:33:30
问题 in a multiple inheritance setting such as laid out in, how can I use super() and also handle the case when the signature of the function changes between classes in the hierarchy? i.e. can I rewrite this example (in python3) to work with super() ? example was taken from the article super() considered harmful article class A(): def __init__(self): print("A") class B(object): def __init__(self): print("B") class C(A): def __init__(self, arg): print("C","arg=",arg) A.__init__(self) class D(B):

Python 2.7 __init__() takes exactly 2 arguments (3 given)

偶尔善良 提交于 2019-12-20 04:59:15
问题 I've got these classes. Person is the parent class and Student is the child class: class Person(object): def __init__(self, name): self.name = name class Student(Person): def __init__(self, avr, name): self.avr = avr super(Student, self).__init__(self, name) I get this error when I try to make an instance of Student : __init__() takes exactly 2 arguments (3 given) What is wrong with my code? 回答1: If you are using super, you don't pass self to the target method. It is passed implicitly. super

Can a java subclass's private final field be initialized before the super constructor completes?

☆樱花仙子☆ 提交于 2019-12-20 03:26:46
问题 I have a pair of classes looking like this; public abstract class Class1 { //... public Class1() { //... function2(); //... } protected abstract void function2(); } public class Class2 implements Class1 { private final OnSomethingListener mOnSomethingListener = new OnSomethingListener() { @Override onSomething() { doThatOtherThing(); } } protected void function2() { //uses mOnSomethingListener //however mOnSomethingListener is null when this function is called from super() //... } public