super

When extending a trait within a trait, what does 'super' refer to?

只愿长相守 提交于 2019-12-03 23:28:08
I want to to extend a trait within a trait, like this: trait NodeTypes { trait Node { def allNodesHaveThis: Int } } trait ScrumptiousTypes extends NodeTypes { trait Node extends super.Node { def scrumptiousness: Int } } trait YummyTypes extends NodeTypes { trait Node extends super.Node { def yumminess: Int } } object Graph extends NodeTypes with ScrumptiousTypes with YummyTypes { case class Node() extends super.Node { override def allNodesHaveThis = 1 override def scrumptiousness = 2 // error: ScrumptiousTypes.Node has been disinherited override def yumminess = 3 } } If this works, it would be

Objective C: Difference between self and super

荒凉一梦 提交于 2019-12-03 21:51:03
I am new to Objective C.I am trying aout some example programs.I could not understand how the self and super methods work in objective C. In the pgm below CashTransaction.m [super trackSpending:amount] is called and in CreditCardTransaction.m [self trackSpending:amount] is called.I could not find difference between the self and super.super is for invoking the base class overriden method.and self is for invoking the child class overridden method.This is what my understanding is.please correct me if i'm wrong.Thanks in advace. main.m #import <Foundation/Foundation.h> #import "BudgetObject.h"

Why doesn't OrderedDict use super?

淺唱寂寞╮ 提交于 2019-12-03 16:46:05
We can create an OrderedCounter trivially by using multiple inheritance: >>> from collections import Counter, OrderedDict >>> class OrderedCounter(Counter, OrderedDict): ... pass ... >>> OrderedCounter('Mississippi').items() [('M', 1), ('i', 4), ('s', 4), ('p', 2)] Correct me if I'm wrong, but this crucially relies on the fact that Counter uses super : class Counter(dict): def __init__(*args, **kwds): ... super(Counter, self).__init__() ... That is, the magic trick works because >>> OrderedCounter.__mro__ (__main__.OrderedCounter, collections.Counter, collections.OrderedDict, dict, object) The

Python self and super in multiple inheritance

那年仲夏 提交于 2019-12-03 16:05:11
问题 In Raymond Hettinger's talk "Super considered super speak" at PyCon 2015 he explains the advantages of using super in Python in multiple inheritance context. This is one of the examples that Raymond used during his talk: class DoughFactory(object): def get_dough(self): return 'insecticide treated wheat dough' class Pizza(DoughFactory): def order_pizza(self, *toppings): print('Getting dough') dough = super().get_dough() print('Making pie with %s' % dough) for topping in toppings: print('Adding

Python: 'super' object has no attribute 'attribute_name'

浪子不回头ぞ 提交于 2019-12-03 15:57:50
问题 I am trying to access a variable from the base class. Here's the parent class: class Parent(object): def __init__(self, value): self.some_var = value And here's the child class: class Child(Parent): def __init__(self, value): super(Child, self).__init__(value) def doSomething(self): parent_var = super(Child, self).some_var Now, if I try to run this code: obj = Child(123) obj.doSomething() I get the following exception: Traceback (most recent call last): File "test.py", line 13, in <module>

Different ways of using __init__ for PyQt4

与世无争的帅哥 提交于 2019-12-03 14:00:12
So... I'm working on trying to move from basic Python to some GUI programming, using PyQt4. I'm looking at a couple different books and tutorials, and they each seem to have a slightly different way of kicking off the class definition. One tutorial starts off the classes like so: class Example(QtGui.QDialog): def __init__(self): super(Example, self).__init__() Another book does it like this: class Example(QtGui.QDialog): def __init__(self, parent=None): super(Example, self).__init__(parent) And yet another does it this way: class Example(QtGui.QDialog): def__init__(self, parent=None): QtGui

Difference for <? super/extends String> in method and variable declaration

风格不统一 提交于 2019-12-03 12:28:00
问题 Given: import java.util.*; public class Hancock { //insert code here list.add("foo"); } } Which two code fragments, inserted independently at line 5, will compile without warnings? (Choose two) A. public void addString(List list) { B. public void addString(List<String> list) { C. public void addString(List<? super String> list) { D. public void addString(List<? extends String> list) { Correct answers are B & C. Answers A and B are quite clear for me. For the answers C & D i know which way the

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

冷暖自知 提交于 2019-12-03 12:02:25
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. 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, then the returned method would be unbound. Methods are just objects, but they can be bound or unbound. An

Using super() in nested classes

孤街浪徒 提交于 2019-12-03 10:52:57
问题 Imagine this: class A(object): class B(object): def __init__(self): super(B, self).__init__() This creates an error: NameError: global name B is not defined. I've tried A.B , but then it says that A is not defined. Update: I've found the problem. I've had a class like this: class A(object): class B(object): def __init__(self): super(B, self).__init__() someattribute = B() In that scope, A isn't defined yet. 回答1: I'm not sure why A.B is not working correctly for you, as it should.. Here's some

calling init for multiple parent classes with super? [duplicate]

馋奶兔 提交于 2019-12-03 10:37:36
This question already has answers here : Can Super deal with multiple inheritance? (2 answers) Possible Duplicate: Can Super deal with multiple inheritance? Python inheritance? I have a class structure (below), and want the child class to call the __init__ of both parents. Is this possible to do in a 'super' way or is it just a terrible idea? class Parent1(object): def __init__(self): self.var1 = 1 class Parent2(object): def _init__(self): self.var2 = 2 class Child(Parent1, Parent2): def __init__(self): ## call __init__ of Parent1 ## call __init__ of Parent2 ## super(Child, self).__init__()