Kung Fu Kung Fu

Fujian White Crane Kung Fu

Zope Zope

What I have and am doing with Zope

Photos Photos

Photoalbum, both old and new.

Receptsamlingen Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me Contact me

My contact details and how to contact me.

  Mobile version of this page Mobile version of this page


 

Python new-style classes and the super() function

python classes, new style, new-style, classes, super

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

fijal - 12th July 2008  [«« Reply to this]
Super is not a function, it's a type (surprise)
Paul Hildebrandt - 12th July 2008  [«« Reply to this]
Property is one of my favorite things about new style classes. They can make something akward seem more pythonic.
Peter Bengtsson - 13th July 2008   [«« Reply to this]
I like the syntax and the look of them but rarely use it. I find it a little bit a solution to a problem that isn't really a problem. In the real world you either just leave the attributes as they are and/or you write full-blown attribute getters/setters that mix in business logic or other legacy fixing logic.
fdfdgdfg - 13th July 2008  [«« Reply to this]
You *can* use super, but you shouldn't.

http://fuhm.net/super-harmful/
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.