2.3. New and Old Style Class

2.3.1. Rationale

2.3.2. 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.3.3. 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.3.4. 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

Listing 2.154. Python 2
class Astronaut:
    """Old style class"""


class Astronaut(object):
    """New style class"""
Listing 2.155. Python 3
class Astronaut:
    """New style class"""


class Astronaut(object):
    """New style class"""

2.3.5. Assignments

Todo

Create assignments