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

126 views
Skip to first unread message

Carles Pina i Estany

unread,
Nov 30, 2021, 5:14:51 PM11/30/21
to django...@googlegroups.com

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

David Nugent

unread,
Nov 30, 2021, 7:32:53 PM11/30/21
to django...@googlegroups.com
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 :-)

I would return the result from super()....  It may return None, but so do you in effect, so all good. return clearly marks the end of block execution.

Regards, David

אורי

unread,
Dec 1, 2021, 11:41:04 AM12/1/21
to django...@googlegroups.com
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.


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20211130221344.GA15295%40pina.cat.
Reply all
Reply to author
Forward
0 new messages