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