super

Python's super(), abstract base classes, and NotImplementedError

大憨熊 提交于 2019-12-03 07:56:26
问题 Abstract base classes can still be handy in Python. In writing an abstract base class where I want every subclass to have, say, a spam() method, I want to write something like this: class Abstract(object): def spam(self): raise NotImplementedError The challenge comes in also wanting to use super() , and to do it properly by including it in the entire chain of subclasses. In this case, it seems I have to wrap every super call like the following: class Useful(Abstract): def spam(self): try:

Why is super used so much in PySide/PyQt?

♀尐吖头ヾ 提交于 2019-12-03 07:24:49
问题 Short version (tl;dr) I am learning PySide, and most online tutorials use super to initialize UI elements. Is this important (i.e., more scalable), or is it a matter of taste? Clarification : as I make more clear in the detailed version, this is not another generic thread asking when to use super (this has been done before). Rather, given the number of PySide tutorials that use super instead of <class>.__init__ , I am trying to figure out if using super is standard in PySide applications? If

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

拥有回忆 提交于 2019-12-03 06:21:49
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> obj.doSomething() File "test.py", line 10, in doSomething parent_var = super(Child, self).some_var

Using super() in nested classes

泄露秘密 提交于 2019-12-03 02:26:27
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. I'm not sure why A.B is not working correctly for you, as it should.. Here's some shell output that works: >>> class A(object): ... class B(object): ... def __init__(self): ... super(A.B,

Second parameter of super()?

大憨熊 提交于 2019-12-03 02:16:40
A colleague of mine wrote code analogous to the following today, asked me to have a look, and it took me a while to spot the mistake: class A(): def __init__(self): print('A') class B(A): def __init__(self): super(B).__init__() b = B() The problem here is that there's no self parameter to super() in B 's constructor. What surprised me is that absolutely nothing happens in this case, i.e. no error, nothing. What does the super object created by super(B) contain? As an object, it clearly has a constructor, so that's what gets called, but how is that object related to B ? In particular, why is

TypeError: Super does not take Key word arguments?

三世轮回 提交于 2019-12-03 02:03:39
First, here is my code: class Enemy(): def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage def is_alive(self): """Checks if alive""" return self.hp > 0 class WildBoar(Enemy): def __init__(self): super(WildBoar, name="Wild Boar", hp=10, damage=2).__init__() class Marauder(Enemy): def __init__(self): super(Marauder, name="Marauder", hp=20, damage=5).__init__() class Kidnappers(Enemy): def __init__(self): super(Kidnappers, name="The Kidnappers", hp=30, damage=7).__init__() When I compile this I get this error: super(WildBoar, name="Wild Boar", hp=10, damage=2)

Python's super(), abstract base classes, and NotImplementedError

牧云@^-^@ 提交于 2019-12-02 23:04:27
Abstract base classes can still be handy in Python. In writing an abstract base class where I want every subclass to have, say, a spam() method, I want to write something like this: class Abstract(object): def spam(self): raise NotImplementedError The challenge comes in also wanting to use super() , and to do it properly by including it in the entire chain of subclasses. In this case, it seems I have to wrap every super call like the following: class Useful(Abstract): def spam(self): try: super(Useful, self).spam() except NotImplementedError, e: pass print("It's okay.") That's okay for a

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

╄→尐↘猪︶ㄣ 提交于 2019-12-02 22:19:02
问题 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

Why is super used so much in PySide/PyQt?

戏子无情 提交于 2019-12-02 22:08:09
Short version (tl;dr) I am learning PySide, and most online tutorials use super to initialize UI elements. Is this important (i.e., more scalable), or is it a matter of taste? Clarification : as I make more clear in the detailed version, this is not another generic thread asking when to use super (this has been done before). Rather, given the number of PySide tutorials that use super instead of <class>.__init__ , I am trying to figure out if using super is standard in PySide applications? If so, is it because the circumstances where super is called for (involving resolving inheritances) come

Python super() behavior not dependable

走远了吗. 提交于 2019-12-02 18:45:51
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 Retrieval class, and then itself as an object, which is, as far as I'm aware,exactly how super() is