super

Is there a reason not to send super().__init__() a dictionary instead of **kwds?

≯℡__Kan透↙ 提交于 2019-12-01 23:39:13
问题 I just started building a text based game yesterday as an exercise in learning Python (I'm using 3.3). I say "text based game," but I mean more of a MUD than a choose-your-own adventure. Anyway, I was really excited when I figured out how to handle inheritance and multiple inheritance using super() yesterday, but I found that the argument-passing really cluttered up the code, and required juggling lots of little loose variables. Also, creating save files seemed pretty nightmarish. So, I

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

风格不统一 提交于 2019-12-01 21:28:31
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]: (__main__.Son, __main__.Father, __main__.GrandFather, __main__.Mother, object) In [2]: Father.__mro__ Out[2

python: super()-like proxy object that starts the MRO search at a specified class

不想你离开。 提交于 2019-12-01 19:52:12
According to the docs, super(cls, obj) returns a proxy object that delegates method calls to a parent or sibling class of type cls I understand why super() offers this functionality, but I need something slightly different: I need to create a proxy object that delegates methods calls (and attribute lookups) to class cls itself; and as in super , if cls doesn't implement the method/attribute, my proxy should continue looking in the MRO order (of the new not the original class). Is there any function I can write that achieves that? Example: class X: def act(): #... class Y: def act(): #... class

Can I use Python 3 super() in Python 2.5.6?

坚强是说给别人听的谎言 提交于 2019-12-01 15:07:18
Can I use clean Python 3 super() syntax in Python 2.5.6? Maybe with some kind of __future__ import? donkopotamus You cannot use a bare super() call that contains no type/class. Nor can you implement a replacement for it that will work. Python 3.x contains special support to enable bare super() calls (it places a __class__ cell variable in all functions defined within a class - see PEP 3135 Update As of Python 2.6+, bare super() calls can be used via the future Python package. See posita's answer for an explanation. posita I realize this question is old, and the selected answer may have been

Inheriting from a namedtuple base class - Python

你。 提交于 2019-12-01 14:58:56
This question is asking the opposite of Inherit namedtuple from a base class in python , where the aim is to inherit a subclass from a namedtuple and not vice versa. In normal inheritance, this works: class Y(object): def __init__(self, a, b, c): self.a = a self.b = b self.c = c class Z(Y): def __init__(self, a, b, c, d): super(Z, self).__init__(a, b, c) self.d = d [out]: >>> Z(1,2,3,4) <__main__.Z object at 0x10fcad950> But if the baseclass is a namedtuple : from collections import namedtuple X = namedtuple('X', 'a b c') class Z(X): def __init__(self, a, b, c, d): super(Z, self).__init__(a, b

I can't get super() to work in python 2.7

浪子不回头ぞ 提交于 2019-12-01 14:23:06
With a simple pair of classes, I cannot get super working: class A(object): q = 'foo' class B(A): q = 'bar' def __init__(self): self.a = super(A, self).q a = B() errors like so: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-210-802575054d17> in <module>() 5 def __init__(self): 6 self.a = super(A, self).q ----> 7 a = B() <ipython-input-210-802575054d17> in __init__(self) 4 q = 'bar' 5 def __init__(self): ----> 6 self.a = super(A, self).q 7 a = B() AttributeError: 'super' object has no attribute 'q' I

Inheriting from a namedtuple base class - Python

随声附和 提交于 2019-12-01 13:46:10
问题 This question is asking the opposite of Inherit namedtuple from a base class in python , where the aim is to inherit a subclass from a namedtuple and not vice versa. In normal inheritance, this works: class Y(object): def __init__(self, a, b, c): self.a = a self.b = b self.c = c class Z(Y): def __init__(self, a, b, c, d): super(Z, self).__init__(a, b, c) self.d = d [out]: >>> Z(1,2,3,4) <__main__.Z object at 0x10fcad950> But if the baseclass is a namedtuple : from collections import

Does calling super() cause further methods in the parent class to be used?

谁说我不能喝 提交于 2019-12-01 11:13:43
问题 I have a question about super that I wanted confirmed. Consider the following code example: class InFasionHello def hello person greet person.name end def greet name p 'Dude, hey ' + name end end class OldFasionedHello < InFasionHello def hello person greet person.name if person.old_fashioned super(person) if !person.old_fashioned end def greet name p 'Good Day to you ' + name + '!' end end My question is, if I was using OldFasionedHello , would infasionHello use the local greet to it self or

set title to JFrame

偶尔善良 提交于 2019-12-01 06:28:57
I am new to Java. My problem is that I have a class names MyClassExp. I have extended it from JFrame. Inside the class, I initiate an object of another class named TabbedFrame. TabbedFrame also extends a class DemoFrame . In DemoFrame, I have the title of the page set using keyword 'super' like: super("Some Title"); Now when I run my MyClassExp, even after creating a JFrame as: new JFrame("New Title"); I'm still getting the same title i.e Some Title. Is there any way to solve this problem? I've tried a lot to solve it but failed :'( Use the API method public void setTitle(String title) . -

How does Python's super() actually work, in the general case?

本秂侑毒 提交于 2019-12-01 06:20:12
问题 There are a lot of great resources on super() , including this great blog post that pops up a lot, as well as many questions on Stack Overflow. However I feel like they all stop short of explaining how it works in the most general case (with arbitrary inheritance graphs), as well as what is going on under the hood. Consider this basic example of diamond inheritance: class A(object): def foo(self): print 'A foo' class B(A): def foo(self): print 'B foo before' super(B, self).foo() print 'B foo