[Tutor] Calling super in __init__
Kent Johnson
kent37 at tds.net
Mon May 19 12:40:42 CEST 2008
On Mon, May 19, 2008 at 3:27 AM, wcyee <wcyeee at gmail.com> wrote:
> Hi - I wrote a custom exception class as follows:
>
> class CustomError(Exception):
> def __init__(self, msg):
> super(CustomError, self).__init__(self, msg)
Should be
super(CustomError, self).__init__(msg)
i.e. don't repeat 'self'.
> According to the documentation, "super() only works with new-style classes".
> Is the problem that Exception is an "old style" class (I'm still learning
> what this means)?
No
> If so, how can I tell when I'm up against an "old style"
> class when using python?
The type of a new-style class is 'type' (unless it has a custom
metaclass). The type of an old-style class is 'classobj':
In [20]: type(Exception)
Out[20]: <type 'type'>
In [21]: class Foo: pass
....:
In [22]: type(Foo)
Out[22]: <type 'classobj'>
New-style classes have a __getattribute__() special method which is
missing in old-style classes, so another way to tell is to look for
__getattribute__.
Kent
More information about the Tutor
mailing list