D
Daniel Klein
Why does 'super' require the classname as an argument?
For example, I am subclassing 'list' and overriding all of the methods
that allow a new object to be added to the list as follows...
class ValidatedList(list):
# We only need to override methods which add objects to the list.
def __new__(cls, alist = []):
for objekt in alist:
_validate(objekt)
return list.__new__(cls, alist)
def append(self, objekt):
_validate(objekt)
# list.append(self, objekt)
super(ValidatedList,self).append(objekt)
def extend(self, alist):
for objekt in alist
_validate(objekt)
list.extend(self, alist)
def insert(self, index, objekt):
_validate(objekt)
list.insert(self, index, objekt)
def _validate(objekt):
# do some validation and raise a TypeError (or whatever)
# if it does not pass the validation.
I understand the implications of diamond shaped hierarchies. But the
question remains as to why it is necessary to for the class name to be
the first argument to 'super'. Is there a scenario where you would use
a different class name?
In Smalltalk, a message send to 'super' simply means to send a message
to self but start looking in the superclass first for the method. I
realize Smalltalk is a single-inheritance language, but doesn't
explain why, in Python, you need to specify the class name as a
parameter.
As an aside, you will notice in the 'append' method, there are two
ways to do the same thing, and either way could be used in the other
methods as well. Which brings up my second question: Is one way more
correct/efficient/better/pick your adjective? I'm very inclined to use
the 'super' method cos that means I won't have to worry about changing
things in the future.
I did spend some time Googling but didn't find anything.
Thanks for your time,
Daniel Klein
For example, I am subclassing 'list' and overriding all of the methods
that allow a new object to be added to the list as follows...
class ValidatedList(list):
# We only need to override methods which add objects to the list.
def __new__(cls, alist = []):
for objekt in alist:
_validate(objekt)
return list.__new__(cls, alist)
def append(self, objekt):
_validate(objekt)
# list.append(self, objekt)
super(ValidatedList,self).append(objekt)
def extend(self, alist):
for objekt in alist
_validate(objekt)
list.extend(self, alist)
def insert(self, index, objekt):
_validate(objekt)
list.insert(self, index, objekt)
def _validate(objekt):
# do some validation and raise a TypeError (or whatever)
# if it does not pass the validation.
I understand the implications of diamond shaped hierarchies. But the
question remains as to why it is necessary to for the class name to be
the first argument to 'super'. Is there a scenario where you would use
a different class name?
In Smalltalk, a message send to 'super' simply means to send a message
to self but start looking in the superclass first for the method. I
realize Smalltalk is a single-inheritance language, but doesn't
explain why, in Python, you need to specify the class name as a
parameter.
As an aside, you will notice in the 'append' method, there are two
ways to do the same thing, and either way could be used in the other
methods as well. Which brings up my second question: Is one way more
correct/efficient/better/pick your adjective? I'm very inclined to use
the 'super' method cos that means I won't have to worry about changing
things in the future.
I did spend some time Googling but didn't find anything.
Thanks for your time,
Daniel Klein