1

Is there any differences between these to declare an object?

class MyStuff(object):

def __init__(self):
    self.tangerine = "And now a thousand years between"

and

class MyStuff:

def __init__(self):
    self.tangerine = "And now a thousand years between"
Nhan Ly
  • 95
  • 1
  • 4
  • 12
  • Read the data model docs, specifically the __init__ method is relevant here: https://docs.python.org/2/reference/datamodel.html#object.__init__ – m0bi5 Feb 03 '15 at 06:26
  • If you're using Python 3.x, there's no difference as it only has new-style classes... if you're using Python 2.x, then the first example results in a new-style class and the second an old-style class... that's it – Jon Clements Feb 03 '15 at 06:28

1 Answers1

0

Yes, there is a difference. At least, there's a difference in Python 2.

class MyStuff: creates an old-style class,

class MyStuff(object): creates a new-style class.

In Python 3, all classes are new-style.

From https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes

Classes and instances come in two flavors: old-style (or classic) and new-style.

Up to Python 2.1 the concept of class was unrelated to the concept of type, and old-style classes were the only flavor available. For an old-style class, the statement x.__class__ provides the class of x, but type(x) is always <type 'instance'>. This reflects the fact that all old-style instances, independent 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 practical 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.

While this manual aims to provide comprehensive coverage of Python’s class mechanics, it may still be lacking in some areas when it comes to its coverage of new-style classes. Please see https://www.python.org/doc/newstyle/ for sources of additional information.

Old-style classes are removed in Python 3, leaving only new-style classes.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182