Opened 10 years ago

Closed 10 years ago

#919 closed defect (fixed)

Do not use super(self.__class__)

Reported by: Elrond Owned by:
Priority: major Milestone:
Component: programming Keywords: bitesized
Cc: berkerpeksag Parent Tickets:

Description

Some places in the code use something like

class B(A):
    def __init__(self):
        super(self.__class__, self).__init__()

This seems to work, until someone creates a subclass.

Please fix all of those.

Either use

    def __init__(self):
        A.__init__(self)

or

    def __init__(self):
        super(B, self).__init__()

Change History (3)

comment:1 by Odin Hørthe Omdal (Velmont), 10 years ago

Hmm? super(type(self), self).__init__() seem to work as intended here? And type() is basically the same, no?

comment:2 by berkerpeksag, 10 years ago

Cc: berkerpeksag added

No, because type(self) and self.__class__ are actually the same thing.

Here's a simple reproducer:

>>> class A(object):
...   pass
...
>>> class B(A):
...   def __init__(self):
...     print(super(type(self), self))
...     print(super(self.__class__, self))
...     print(super(B, self))
... 
>>> B()
<super: <class 'B'>, <B object>>
<super: <class 'B'>, <B object>>
<super: <class 'B'>, <B object>>
<__main__.B object at 0x7fed65b2dc20>
>>> class C(B):
...   pass
... 
>>> C()
<super: <class 'C'>, <C object>>
<super: <class 'C'>, <C object>>
<super: <class 'B'>, <C object>>
<__main__.C object at 0x7fed65b2dcf0>

I've converted all classes to use new-style classes in my Python 3 branch, so using the super(B, self).__init__() option would be the best choice.

comment:3 by Jessica Tallon, 10 years ago

Resolution: fixed
Status: newclosed

I have grepped through the code and corrected the super calls doing super(self.\_\_type\_\_, self). This should be fixed as of 1a2982d.

Note: See TracTickets for help on using tickets.