Wednesday, December 1, 2021

Re: return or not from super().method(*args, **kwargs)

Hi Carles,

return super(...) is better. Because if the base class decides to return a value sometime in the future, you are all set.

Uri.


On Wed, Dec 1, 2021 at 12:14 AM Carles Pina i Estany <carles@pina.cat> wrote:

Hi django-users,

I have a a question that I don't know where to send. Since it happened
programming in Django and I usually read this list I thought of sending
it here.  I would like to ask if someone has strong feelings or if I
have missed some styling documentation.

This could be a generic Python styling question when re-implementing a
method of a base class. The example below is for models.Model.

Let's say that you had a model such as:

class Person(models.Model):
    name = models.CharField(max_length=100)

    def save(self, *args, **kwargs):
        # some things...

        # Question: do you do:
        super().save(*args, **kwargs)

        # or do you:
        return super().save(*args, **kwargs)

Currently Model.save() returns None.

My thoughts:
-"return super().save(*args, **kwargs)":
 -Good: if some day the base class changes and starts returning
 something: the sub-class is forward-compatible :-)
 -Good: less thinking when implementing the derived method: just do the
 return always instead of having to check the base class method
 -Bad: a reader might assume that Person.save() returns a value (instead of None)
 -?: I haven't looked yet if I could add type hints for the return value of the the sub-class method saying "same as the base class".

-"super().save(*args, **kwargs)":
 -Good: it's clear for the reader that nothing is returned
 -Easy to do the type hints "-> None" if used in the project

Any thoughts? What would you prefer to see?

Currently I tend to go for the "super().save(*args, **kwargs)" (no
unnecessary return) as done in the Django documentation and to be
explicit in the present (it doesn't return anything)... but I have my
own doubts :-)

Part of this happened when I was reading some code and I thought that I
was in a "save()" for a form but I was in the model. For the form I need
to not forget the "return".

Thanks for any thoughts / comments :-)

Cheers,

--
Carles Pina i Estany
https://carles.pina.cat

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20211130221344.GA15295%40pina.cat.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABD5YeHG0jb6JyF%3D3det5-LQAZAgxC2ivqAScx1USVS8G8nfdg%40mail.gmail.com.

No comments:

Post a Comment