Opened 17 years ago

Closed 17 years ago

#5256 closed (fixed)

Python Super and Inheritance is incorrectly written

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

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 by Adrian Holovaty, 17 years ago

Resolution: fixed
Status: newclosed

(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