super

Python super() behavior not dependable

房东的猫 提交于 2019-12-04 07:56:13
问题 For some reason, the super() method is not always behaving as expected, opting to return: TypeError('super(type, obj): obj must be an instance or subtype of type)' I understand what the error means . I do not understand why it is coming up as an error. Here's the snippet of code that is breaking. All objects in the system are new style objects. What's really interesting is that this error does not always show up. I don't know what's causing it. The super() method in Retrieval is passing the

Python multi-inheritance, __init__

你。 提交于 2019-12-04 07:50:09
问题 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

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

六月ゝ 毕业季﹏ 提交于 2019-12-04 07:26:01
问题 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,

Scope and use of super keyword in Java

孤街浪徒 提交于 2019-12-04 07:15:45
问题 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(); } } 回答1: The answer to the original question is simple: There is

How to access Abstract superclass instance variable

限于喜欢 提交于 2019-12-04 06:56:17
问题 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(){

(iphone) what does [super touchesBegan/Moved/Ended] do?

佐手、 提交于 2019-12-04 05:18:20
Most overloaded methods require [super theMethod] call. (For example, [super viewDidLoad]; , [super viewWillAppear]; and [super dealloc]; ) I didn't think twice whether I need [super touchesBegan:withEvent:] call or not, but it seems to play a role somehow. When do I need it and when don't I need it? I'm trying to programmatically cancel touch events when I need to, and it seems to be related to the question I asked. MHC While it all depends on the expected behaviors of the object, let's point out the basic: You call the superclass implementation in an overridden method (you mean overriding,

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

旧巷老猫 提交于 2019-12-04 04:31:06
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 Sub1 constructor calling the super.printThree(); . But I actually get 0 Print Three 3 I know 0 is default

Get “super(): no arguments” error in one case but not a similar case

情到浓时终转凉″ 提交于 2019-12-04 03:48:05
class Works(type): def __new__(cls, *args, **kwargs): print([cls,args]) # outputs [<class '__main__.Works'>, ()] return super().__new__(cls, args) class DoesNotWork(type): def __new__(*args, **kwargs): print([args[0],args[:0]]) # outputs [<class '__main__.doesNotWork'>, ()] return super().__new__(args[0], args[:0]) Works() # is fine DoesNotWork() # gets "RuntimeError: super(): no arguments" As far as I can see, in both cases super._new__ receives the class literal as first argument, and an empty tuple as the 2nd. So why does one give an error and the other not? The zero-argument form of super

Python methods: default parameter values are evaluated ONCE

狂风中的少年 提交于 2019-12-04 01:07:27
问题 I've found a strange issue with subclassing and dictionary updates in New-Style Classes: Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 >>> class a(object): ... def __init__(self, props={}): ... self.props = props ... >>> class b(a): ... def __init__(self, val = None): ... super(b, self).__init__() ... self.props.update({'arg': val}) ... >>> class c(b): ... def __init__(self, val): ... super(c, self).__init__(val) ... >>> b_inst = b(2) >>> b_inst.props {

Extending Generic Abstract Class & Correct Use of Super

99封情书 提交于 2019-12-04 00:21:07
问题 public abstract class AbstractTool<AT extends AbstractThing> { protected ArrayList<AT> ledger; public AbstractTool() { ledger = new ArrayList<AT>(); } public AT getToolAt(int i) { return ledger.get(i); } // More code Which operates on Ledger ... } public class Tool<AT extends AbstractThing> extends AbstractTool { public Tool() { super(); } } How do I correctly call super to pass the AT generic of Tool to the AbstractTool constructor? It seems no matter what I pick AT to be when I declare Tool