4

In python class declaration I can declare a class by few ways. What is a difference between following samples?

class MyClass:
 def __init__(self)
     pass


class MyClass(object):
   def __init__(self)
     pass
1
  • An important question, but one that could have obviously been answered 'self-serve' by reading the docs.. Commented Jan 3, 2012 at 11:09

2 Answers 2

7

The second declaration creates a new-style class. A new-style class is derived from a built-in type, in this case an object. This was introduced in python 2.2 in an effort to unify classes and types. For backward compatibility old-style classes are still the default

Additional read: http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html

Sign up to request clarification or add additional context in comments.

1 Comment

FTR, in Python 3 there is no difference between these 2 syntaxes.
2

The second way creates a "new-style" class. Documentation is admittedly a bit lacking, as mentioned in a couple places on the python website Python Guide 3.3, and here. There's also an essay describing their design by Python's creator (Guido van Rossum), but it's not strictly documentation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.