Opened 8 years ago

Closed 8 years ago

#5256 closed (fixed)

Python Super and Inheritance is incorrectly written

Reported by: Trey <trey@…> Owned by: jacob
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation:
Needs tests: Patch needs improvement:
Easy pickings: UI/UX:

Description

Alright, this one nearly drove me mad. In the custom widget section of the documentation:
http://www.djangoproject.com/documentation/newforms/#custom-widgets

There is the following block of code:

class CommentWidget(forms.TextInput):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('attrs',{}).update({'size': '40'})
        super(forms.TextInput, self).__init__(*args, **kwargs)

This seems rather harmless first and in fact works coincidentally most of the time. However in the Python super keyword the current class should be specified instead of the one being inherited from.

Like this:

class CommentWidget(forms.TextInput):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('attrs',{}).update({'size': '40'})
        super(CommentWidget, self).__init__(*args, **kwargs)

I noticed this issue when inheriting from the Select widget. This brings up another argument that most of the Python community thinks the super keyword causes a lot of weird problems and side affects and should be replaced completely in favor of the following syntax.

class CommentWidget(forms.TextInput):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('attrs',{}).update({'size': '40'})
        forms.TextInput.__init__(self, *args, **kwargs)

Change History (1)

comment:1 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

(In [6003]) Fixed #5256 -- Fixed incorrect use of super() in docs/newforms.txt example. Thanks, trey@…

Note: See TracTickets for help on using tickets.
Back to Top