#21111: generic.View.__init__ should call super
--------------------------------------+------------------------------------
     Reporter:  glin@…                |                    Owner:  gmeno
         Type:  Cleanup/optimization  |                   Status:  closed
    Component:  Generic views         |                  Version:  1.5
     Severity:  Normal                |               Resolution:  wontfix
     Keywords:  afraid-to-commit      |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by glin@…):

 Sorry I couldn't get to my google account so I couldn't react on the
 goggle groups thread.

 Having this example:

 {{{
 class View(object):
     def __init__(self):
         print "View init"
         #super(View, self).__init__()

 class SomeMixin(object):
     def __init__(self):
         print "SomeMixin init"
         super(SomeMixin, self).__init__()

 class MyView(View, SomeMixin):
     def __init__(self):
         print "MyVIew init"
         super(MyView, self).__init__()

 MyView()
 }}}

 With the commented line in the View class, the method
 {{{SomeMixin.__init__}}} is not called, because mro chain is broken,
 that's the reason why I filled the ticket.

 Practical use - for exemple in current mixins, there are instance
 attributes which are not defined in {{{__init__}}}, which is bad, code is
 harder to read when new attributes pop up on unusual places, every checker
 like pylint displays warning like:
 {{{
 W:127,8:ModelFormMixin.form_valid: Attribute 'object' defined outside
 __init__
 }}}
 So in {{{ModelFormMixin}}}, there should be {{{__init__}}} which defines
 'object' attribute ({{{self.object = None}}}), which is IMHO more than
 good practice.

 Also having attribute 'object' in {{{__init__}}}, we could avoid ugly
 constructions like now (also in views/generic/edit.py):
 {{{
 elif hasattr(self, 'object') and self.object is not None:
 }}}

 And that's example in Django itself, I bet there could be lot more in
 various cases of other developers views/mixins, which are hurt by broken
 mro chain.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21111#comment:12>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.1a5d528be82cbb7fb63dfea3580f93ef%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to