Python

Python new-style classes and the super() function


12th of July 2008

I've never really understood the impact of new-style Python classes and what it means to your syntax until now. With new-style classes you can use the super() builtin, otherwise you can't. This works for new-style classes:

 class Farm(object):
    def __init__(self): pass

 class Barn(Farm):
    def __init__(self):
        super(Barn, self).__init__()

If you want to do the same for old-style classes you simply can't use super() so you'll have to do this:

 class Farm:
    def __init__(self): pass

 class Barn(Farm):
    def __init__(self):
        Farm.__init__(self)

Strange that I've never realised this before. The reason I did now was that I had to back-port some code into Zope 2.7 which doesn't support setting security on new-style classes.

Now I need to do some reading on new-style classes because clearly I haven't understood it all.



Comment

Show all 4 comments
 

Commenting is currently disabled in Mobile version