Re: super() is super [was Re: Calling dunder methods manually]

2017-04-15 Thread Gregory Ewing

Steve D'Aprano wrote:

But for the simple cases, using super() in Python 3 couldn't be easier.


The only "simple" use of super() is in the single inheritance
case. But that's also the case where it gains you the least
over an explicit inherited method call.


If you have multiple inheritance, and don't use super(), then your code is
buggy, whether you have realised it or not.

Manually calling your parent class is only acceptable if you can absolutely
guarantee that your class, all its parent classes, and all its subclasses
will ONLY use single inheritance.


I don't agree with that at all. It's quite possible to write
code that uses multiple inheritance, doesn't use super() and
works perfectly correctly.

Someone else might use my class in an inheritance network in
a way that results in misbehaviour, but if it happens, *they*
wrote that bug, not me.

You could argue that I *should* write my code so that anyone
can mix it with anything in any way without having to think
about the consequences, but that sounds more like a moral
judgement than a technical argument.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: super() is super [was Re: Calling dunder methods manually]

2017-04-15 Thread Chris Angelico
On Sun, Apr 16, 2017 at 10:35 AM, Steve D'Aprano
 wrote:
>
>> eaisier to just write the path in long-form.
>
> Easier and wrong.
>
> If you have multiple inheritance, and don't use super(), then your code is
> buggy, whether you have realised it or not.
>
> Manually calling your parent class is only acceptable if you can absolutely
> guarantee that your class, all its parent classes, and all its subclasses
> will ONLY use single inheritance.

Plus, it's fragile in that it names the parent class everywhere.

class A:
def __init__(self): ...
def __add__(self, other): ...
def __or__(self, other): ...

class B(A):
def __init__(self):
A.__init__(self)
def __add__(self, other):
A.__add__(self, other)
...

Every method you subclass-and-call-parent needs to say the name of the
parent class. With super, they all just say super(). I know which one
I'd rather do.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


super() is super [was Re: Calling dunder methods manually]

2017-04-15 Thread Steve D'Aprano
On Sat, 15 Apr 2017 10:50 pm, Rick Johnson wrote:

> Even to this day, i avoid super because the
> semantics are confusing, 

If super() is confusing, it is because *inheritance* is confusing, and that
goes triple for multiple inheritance. If it is not *easy* to use super() to
manage your class' inheritance, that's a sign that your class hierarchy is
complicated and confusing and you're in a nightmare whether you use super()
or not.

But for the simple cases, using super() in Python 3 couldn't be easier. If
you have code that looks something like this:


class MyClass(ParentClass):
def method(self, arg):
result = ParentClass.method(self, arg)


you replace the last line with:

result = super().method(arg)


If you have:

class MyClass(A, B):
def method(self, arg):
A.method(self, arg)
B.method(self, arg)


you replace the last two lines with:

super().method(arg)


See also:

https://rhettinger.wordpress.com/2011/05/26/super-considered-super/



> eaisier to just write the path in long-form.

Easier and wrong.

If you have multiple inheritance, and don't use super(), then your code is
buggy, whether you have realised it or not.

Manually calling your parent class is only acceptable if you can absolutely
guarantee that your class, all its parent classes, and all its subclasses
will ONLY use single inheritance.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list