super

How to access a superclass's class attributes in Python?

落花浮王杯 提交于 2019-12-07 06:22:38
问题 Have a look at the following code: class A(object): defaults = {'a': 1} def __getattr__(self, name): print('A.__getattr__') return self.get_default(name) @classmethod def get_default(cls, name): # some debug output print('A.get_default({}) - {}'.format(name, cls)) try: print(super(cls, cls).defaults) # as expected except AttributeError: #except for the base object class, of course pass # the actual function body try: return cls.defaults[name] except KeyError: return super(cls, cls).get

Does any magic happen when I call `super(some_cls)`?

让人想犯罪 __ 提交于 2019-12-07 01:34:23
问题 While investigating this question, I came across this strange behavior of single-argument super : Calling super(some_class).__init__() works inside of a method of some_class (or a subclass thereof), but throws an exception when called anywhere else. Code sample: class A(): def __init__(self): super(A).__init__() # doesn't throw exception a = A() super(A).__init__() # throws exception The exception being thrown is Traceback (most recent call last): File "untitled.py", line 8, in <module> super

In Java type arguments, does <? extends E> mean strictly subtypes only? or would E also suffice?

白昼怎懂夜的黑 提交于 2019-12-06 23:47:10
问题 In Java type arguments, does mean strictly subtypes only? or would E also suffice? 回答1: Yes, super and extends gives inclusive lower and upper bounds respectively. Here's a quote from Angelika Langer's Generics FAQ: What is a bounded wildcard? A wildcard with an upper bound looks like ? extends Type and stands for the family of all types that are subtypes of Type , type Type being included . Type is called the upper bound . A wildcard with a lower bound looks like ? super Type and stands for

how does metaclass work with the MRO list when super() is called?

孤街浪徒 提交于 2019-12-06 14:28:43
I'm really confused by the following code sample: class Meta_1(type): def __call__(cls, *a, **kw): # line 1 print("entering Meta_1.__call__()") print(cls) # line 4 print(cls.mro()) # line 5 print(super(Meta_1, cls).__self__) # line 6 rv = super(Meta_1, cls).__call__(*a, **kw) # line 7 print("exiting Meta_1.__call__()") return rv class Car(object, metaclass=Meta_1): def __new__(cls, *a, **kw): print("Car.__new__()") rv = super(Car, cls).__new__(cls, *a, **kw) return rv def __init__(self, *a, **kw): print("Car.__init__()") super(Car,self).__init__(*a, **kw) if __name__ == '__main__': c = Car()

Java wildcards and generics ? super T and ? extends T

最后都变了- 提交于 2019-12-06 12:29:46
问题 when dealing with wildcards such as setting/adding a generic item to a certain container is it suggested to use something like this? void add(List<? super T> someList,someitem){ someList.add(someItem); } and when retrieving an item it is suggested to use something like this <T> void f1(List<? extends T> obj, T item) { obj.add(item); } What is the principle behind this? and when will I know if I should use this ? 回答1: you should have a look at the explanation of PECS principle What is PECS

Dart inheritance and super constructor

倖福魔咒の 提交于 2019-12-06 11:09:15
I am trying to define an abstract parent class Event and a concrete child PlaceChangeEvent : abstract class Event { String name; DateTime occurredAt; int handleCount; Event(this.name, this.occurredAt, [this.handleCount = 0]); } class PlaceChangeEvent implements Event { Place place; PlaceChangeEvent(Place place) : super(place.name, DateTime.now()); } The PlaceChangeEvent is giving me 2 compiler warnings: The first is on the class name itself: Missing inherited members: 'Event.name', 'Event.handleCount' and 'Event.occurredAt' The second is on the call to super : 0 positional arguments expected,

Android RecyclerView: Super Fast ListView 超级快速的Lis

℡╲_俬逩灬. 提交于 2019-12-06 08:59:22
Android RecyclerView: Super Fast ListView 原文来自我的微信公众号: longkai_1991 先上图,看效果: 前几天刚release完公司的一个项目,有了点时间,于是就想找一些有意思的东西学习一下,顺便运用在项目之中。看到iOS的同事们在谈论iOS8的xx特性时,我突然也有想在公司项目的下一个版本中添加Android L版本的特性。 六月底的时候收看Google io时,当时对Android新的设计语言,Material Design,没什么太大的好感,感觉色彩一坨一坨的,好难看的样子,当时觉得亮点就是新的ART运行时环境和一些酷炫的动画效果。再后来,8月初的时候,自己出于好奇真的拿Nexus 5安装了一个L的预览版,体验很差...好多软件都还是holo的,反正觉得不是很期待就是啦。 回到重点,下载好最新的SDK,你会发现在 ANDROID_HOME/extras/android/m2repository/com/android/support 下面多了不少兼容库, cardview , support-annotations , recyclerview-v7 ,眼前一亮吧~这回,Google真的是拿出了好多东西呀,赞,尤其是cardview和recyclerview这两个新的控件,这个在Google最新的Material

demystify super in python?

戏子无情 提交于 2019-12-06 07:04:57
问题 I was trying to understand how super works in python and tried the following example: class A(object): def __init__(self): print "in A's init" class B(object): def __init__(self): print "in B's init" class C(A,B): def __init__(self): super(C,self).__init__() print "In C" if __name__=="__main__": c=C() fairly simple.. And I tried the following super calls(displayed with the results here): >>> super(B,c).__init__() >>> super(B,c).__init__() >>> super(A,c).__init__() in B's init >>> super(A,c)._

Explicit passing of Self when calling super class's __init__ in python

烈酒焚心 提交于 2019-12-06 04:12:04
This question is in relation to posts at What does 'super' do in Python? , How do I initialize the base (super) class? , and Python: How do I make a subclass from a superclass? which describes two ways to initialize a SuperClass from within a SubClass as class SuperClass: def __init__(self): return def superMethod(self): return ## One version of Initiation class SubClass(SuperClass): def __init__(self): SuperClass.__init__(self) def subMethod(self): return or class SuperClass: def __init__(self): return def superMethod(self): return ## Another version of Initiation class SubClass(SuperClass):

Java set super instance of instance

血红的双手。 提交于 2019-12-06 02:21:36
I might just be unable to google for the right words, but I can't find an answer to the following question. Is it possible to explicitly set the superclass of a new class instance. E.g. I have a SuperClazz instance and want to create a new instance of Clazz which extends SuperClazz . Can I just do something like this (the code is just what I want to do, it doesn't compile and is not correct): class Clazz extends SuperClazz{ Clazz(SuperClazz superInstance){ this.super = superInstance; } } The super class is always instantiated implicitly, so you cannot do it — "plant" the super class inside an