2.2. New and Old Style Class

  • Old Style classes - Not existing in Python 3

  • In Python 3 class will always produce new style class

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

  • https://docs.python.org/dev/tutorial/classes.html

2.2.1. Old Style classes

  • Not existing in Python 3

  • Don't inherit from object

  • In Python 3, there are no old-style classes, and this code will produce new style class

2.2.2. New Style Class

  • Introduced in Python 2.2

  • In Python 3 this is the only way

  • Inherit from object or from another new style class

  • Ability to subclass most built-in types

  • super() added

  • MRO changed

  • descriptors added

  • new style class objects cannot be raised unless derived from Exception

  • __slots__ added

2.2.3. Definition

  • Old style classes are only in Python 2

  • New style classes works in Python 2 (when inherit from object)

  • In Python 3 all classes always inherit from object, hence they are new style classes

# Python 2.x
# old style class
# inherits from type

class MyClass:
    pass
# Python > 2.2
# new style class
# inherits from object

class MyClass(object):
    pass
# Python 3.x
# meta class
# inherits from type

class MyClass(metaclass=type):
    pass
# Python 3.x
# new style class
# inherits from object

class MyClass:
    pass
# Python 3.x
# new style class
# inherits from object

class MyClass(object):
    pass