super

Python multi-inheritance, __init__

元气小坏坏 提交于 2019-12-02 17:32:00
Regarding multiple parent inheritance, when I call the super . __init__ , why doesn't parent2's __init__ function get called? Thanks. class parent(object): var1=1 var2=2 def __init__(self,x=1,y=2): self.var1=x self.var2=y class parent2(object): var4=11 var5=12 def __init__(self,x=3,y=4): self.var4=x self.var5=y def parprint(self): print self.var4 print self.var5 class child(parent, parent2): var3=5 def __init__(self,x,y): super(child, self).__init__(x,y) childobject = child(9,10) print childobject.var1 print childobject.var2 print childobject.var3 childobject.parprint() Output is 9 10 5 11 12

Python, Overriding an inherited class method

╄→尐↘猪︶ㄣ 提交于 2019-12-02 14:22:46
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 Field's buildField() : "TypeError: buildField() takes exactly 2 arguments (1 given)." I expected

“MetaClass”, “__new__”, “cls” and “super” - what is the mechanism exactly?

假装没事ソ 提交于 2019-12-02 13:59:45
I have read posts like these: What is a metaclass in Python? What are your (concrete) use-cases for metaclasses in Python? Python's Super is nifty, but you can't use it But somehow I got confused. Many confusions like: When and why would I have to do something like the following? # Refer link1 return super(MyType, cls).__new__(cls, name, bases, newattrs) or # Refer link2 return super(MetaSingleton, cls).__call__(*args, **kw) or # Refer link2 return type(self.__name__ + other.__name__, (self, other), {}) How does super work exactly? What is class registry and unregistry in link1 and how exactly

Scope and use of super keyword in Java

我的未来我决定 提交于 2019-12-02 13:44:51
Why can't I access the parent class variable with the super keyword? With the below code, the output is: feline cougar c c class Feline { public String type = "f "; public Feline() { System.out.print("feline "); } } public class Cougar extends Feline { public Cougar() { System.out.print("cougar "); } void go() { type = "c "; System.out.print(this.type + super.type); } public static void main(String[] args) { new Cougar().go(); } } The answer to the original question is simple: There is only one variable called type. Its initial value gets overwritten by c. Remember that there is only one

How to access Abstract superclass instance variable

白昼怎懂夜的黑 提交于 2019-12-02 13:12:01
So I have two classes: Property and Houses . Property is the abstract super class and Houses is its subclass. Here is the code for Property public abstract class Property{ String pCode; double value; int year; public Property(String pCode, double value , int year){ this.pCode = pCode; this.value = value; this.year = year; } public Property(){ pCode = ""; value = 0; year = 0; } public abstract void depreciation(); //Accessors private String getCode(){ return pCode; } private double getValue(){ return value; } private int getYear(){ return year; } //Mutators private void setCode(String newCode){

How to “insert” code before this(…) or super(…)?

瘦欲@ 提交于 2019-12-02 11:55:27
Is there any way to implement preliminary calculations before an invocation of super(...) or this(...) constructor? Consider the following example: public class Test { private final int n; private final int m; private final int[] store; public Test(int n, int m) { /* This is common (most generic) constructor of the class Test. It is desirable to invoke it via this(...) call from any other constructor of this class since it contains some common initialization tasks, which are better to concentrate in one place instead of scattering them throught the class, due to maintainability and security

Accessing indirect super class variable that has been hidden in a third extended class

。_饼干妹妹 提交于 2019-12-02 06:09:57
Suppose i have a code as below : class A { int a = 1; } class B extends A { int a = 2; } class C extends B { int a = 3; void print_it() { int a = 4; // Local variable "a" to the " print_it " method System.out.println(a); //Should be 4 System.out.println(this.a); //Should be 3 System.out.println(super.a); //Should be 2 System.out.println("HOW DO I PRINT \" a \" OF THE \" CLASS A \" "); //I need to print 1 } public static void main(String[] argue) { C obj = new C(); obj.print_it(); } } How can i access "a" of the "class A" indirectly inherited to "class C".I know i can create an object of the "

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

我与影子孤独终老i 提交于 2019-12-02 04:26:43
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? davidism If you are using super, you don't pass self to the target method. It is passed implicitly. super(Student, self).__init__(name) That's 2 arguments total (self, name). When you passed self , that

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

微笑、不失礼 提交于 2019-12-02 01:00:56
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 Class2() { super(); } } I assume the listener is null because I am effectively referencing it from super()

How does `super` interacts with a class's `__mro__` attribute in multiple inheritance?

假装没事ソ 提交于 2019-12-01 23:43:16
问题 Today, I read the official doc of super. In which it mentioned multiple inheritance will be decided by the __mro__ attribute of a class. So I did a bit experiment, but its result surprised me. # CODE PART class GrandFather(object): def p(self): print "I'm old." class Father(GrandFather): def p(self): print "I'm male." class Mother(object): def p(self): print "I'm female." class Son(Father, Mother): def p(self): print "busy, busy, crwaling. " # EXPERIMENT PART In [1]: Son.__mro__ Out[1]: (_