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.

Comments

fijal

Super is not a function, it's a type (surprise)

Paul Hildebrandt

Property is one of my favorite things about new style classes. They can make something akward seem more pythonic.

Peter Bengtsson

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

You *can* use super, but you shouldn't.

http://fuhm.net/super-harmful/

Talat Parwez

In older version of python; for super keyword we use to give as:
    super(current_class_name, self).method_name()

But for newer version of python or say new style classes we can simply give:
    super().method_name()

What it'll do is to pass the control to the parent class instead of performing execution for current class.

Having any problem; Ping me @talatparwez27@gmail.com

Your email will never ever be published.

Previous:
Use Javascript to prevent spambots July 9, 2008 Web development
Next:
That should last a couple of weeks August 7, 2008
Related by category:
How I run standalone Python in 2025 January 14, 2025 Python
get in JavaScript is the same as property in Python February 13, 2025 Python
How to resolve a git conflict in poetry.lock February 7, 2020 Python
Best practice with retries with requests April 19, 2017 Python
Related by keyword:
get in JavaScript is the same as property in Python February 13, 2025 Python, JavaScript
Newfound love of @staticmethod in Python July 2, 2012 Python
Eloquent Javascript by Marijn Haverbeke February 25, 2011 JavaScript
Mixing in new-style classes in Zope 2.7 April 9, 2008 Zope