Difference Between Old Style and New Style Classes in Python

What is the difference between old style and new style classes in Python?

From New-style and classic classes:

Up to Python 2.1, old-style classes were the only flavour available to the user.

The concept of (old-style) class is unrelated to the concept of type:
if x is an instance of an old-style class, then x.__class__
designates the class of x, but type(x) is always <type
'instance'>
.

This reflects the fact that all old-style instances, independently of
their class, are implemented with a single built-in type, called
instance.

New-style classes were introduced in Python 2.2 to unify the concepts of class and type.
A new-style class is simply a user-defined type, no more, no less.

If x is an instance of a new-style class, then type(x) is typically
the same as x.__class__ (although this is not guaranteed – a
new-style class instance is permitted to override the value returned
for x.__class__).

The major motivation for introducing new-style classes is to provide a unified object model with a full meta-model.

It also has a number of immediate benefits, like the ability to
subclass most built-in types, or the introduction of "descriptors",
which enable computed properties.

For compatibility reasons, classes are still old-style by default.

New-style classes are created by specifying another new-style class
(i.e. a type) as a parent class, or the "top-level type" object if no
other parent is needed.

The behaviour of new-style classes differs from that of old-style
classes in a number of important details in addition to what type
returns.

Some of these changes are fundamental to the new object model, like
the way special methods are invoked. Others are "fixes" that could not
be implemented before for compatibility concerns, like the method
resolution order in case of multiple inheritance.

Python 3 only has new-style classes.

No matter if you subclass from object or not, classes are new-style
in Python 3.

Old Style Classes vs New Style Classes

No. In Python 2, to make a class a new-style class, it had to inherit from object (or from some superclass that did). Old-style classes would inherit other old-style classes, but the base ones didn't inherit from object; they were just written class MyClass:.

Mostly, when new classes came out people started writing class MyClass(object): and for 99% of uses everything stayed the same. The differences were in double-underscore methods and other low level details that didn't matter for most code.

The reasoning was to remove the difference between built-in types and user-defined classes; Guido's reasoning can be read here.

And now that is history, Python 3 just has classes. Python 2.2 introduced new-style classes and it was released December 21, 2001. You don't need to know this.

Old-style and new-style classes in Python 2.7

Always subclass "object". Those are new style classes.

  • You are ready for Python 3 that way.

  • Things like .super() work properly that way, should you need them.

How do I implement this python old style class as a new style class

The difference between old-style class declarations and new-style ones is that new-style classes must inherit from object (or some other new-style class). So, to make your classes new-style, just put (object) after the class name:

class forest(object):
# ...

You're also nesting classes, which isn't forbidden in either new or old-style classes, but which is often confusing and unnecessary. Just unindent your Plant and Animal class declarations and you should be fine. You can make them new-style too, if you want, by inheriting from object.

Note that if you're using Python 3, all classes are new-style classes. You don't need to explicitly inherit from object, as the compiler will add that for you if you omit it. It's often a good idea to make it explicit, especially if you use both Python 2 and Python 3 at different times.

Python inheritance old style type in a new style class

Yes, you only have to inherit from object, too:

class MyObj(object, csv.DictWriter):
def __init__(self, f, *args, **kw):
csv.DictWriter.__init__(self, f, *args, **kw)

Which methods are called when an old-style object is created in Python 2.7?

__init__ only exists and is only called if you define it. Since you didn't define __init__, it's not called. When it is defined, it's the only useful point of interception to creating the instance, and most of what you're trying to do works fine.

New-style classes can also use __new__ to hook instance construction (as opposed to initialization), and also allow for metaclasses (which can hook stuff in even crazier ways), but they're not available for old-style classes (defining __new__ doesn't change anything, and using metaclasses implicitly opts in to new-style classes).



Related Topics



Leave a reply



Submit