Opened 17 years ago
Closed 17 years ago
#5256 closed (fixed)
Python Super and Inheritance is incorrectly written
Reported by: | 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)
(In [6003]) Fixed #5256 -- Fixed incorrect use of super() in docs/newforms.txt example. Thanks, trey@…