All classes are new-style classes in Python 3

suggest change

In Python 3.x all classes are new-style classes; when defining a new class python implicitly makes it inherit from object. As such, specifying object in a class definition is a completely optional:

class X: pass
class Y(object): pass

Both of these classes now contain object in their mro (method resolution order):

>>> X.__mro__
(__main__.X, object)

>>> Y.__mro__
(__main__.Y, object)

In Python 2.x classes are, by default, old-style classes; they do not implicitly inherit from object. This causes the semantics of classes to differ depending on if we explicitly add object as a base class:

class X: pass
class Y(object): pass

In this case, if we try to print the __mro__ of Y, similar output as that in the Python 3.x case will appear:

>>> Y.__mro__
(<class '__main__.Y'>, <type 'object'>)

This happens because we explicitly made Y inherit from object when defining it: class Y(object): pass. For class X which does not inherit from object the __mro__ attribute does not exist, trying to access it results in an AttributeError.

In order to ensure compatibility between both versions of Python, classes can be defined with object as a base class:

class mycls(object):
    """I am fully compatible with Python 2/3"""

Alternatively, if the __metaclass__ variable is set to type at global scope, all subsequently defined classes in a given module are implicitly new-style without needing to explicitly inherit from object:

__metaclass__ = type

class mycls:
    """I am also fully compatible with Python 2/3"""

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Incompatibilities moving from Python 2 to Python 3:
* All classes are new-style classes in Python 3
* map

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
39 Incompatibilities moving from Python 2 to Python 3
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve