26

Possible Duplicate:
Old style and new style classes in Python

What is the current state of affairs with new-style and old-style classes in Python 2.7?

I don't work with Python often, but I vaguely remember the issue. The documentation doesn't seem to mention the issue at all: The Python Tutorial: Classes. Do I still need to worry about this? In general, should I declare my classes like the following?

class MyClass:
    pass

or?

class MyClass(object):
    pass
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
User
  • 62,498
  • 72
  • 186
  • 247
  • 2
    @LennartRegebro: that question was asked in 2008, with the accepted answer also in 2008. I was asking what is the situation in 2012 with Python 2.7. The answer was non-obvious to me from reading that question. – User Dec 11 '12 at 09:27
  • Do you have any reason to think that that the answer would have changed from Python 2.6 to Python 2.7? Although that doesn't matter. Whatever your reasons are to post this question, it is an exact duplicate. – Lennart Regebro Dec 11 '12 at 09:29
  • @LennartRegebro: Yes 4 years is a lot of time, a lot can change. Didn't realize Python 2.6 was 4 years ago. – User Dec 11 '12 at 09:32
  • A comment on the original question could be an option in that case, to ask if this is still valid. But IMO, unless you have a reason to suspect it has changed, you don't even need to do that. The answers are really quite clear: Don't use old-style classes, they are only there for backwards compatibility. Why on earth would that answer change for Python 2.7? Would they suddenly decide that old-style classes are actually better? Of course not. – Lennart Regebro Dec 11 '12 at 09:50
  • 1
    @LennartRegebro: Perhaps as someone who follows the Python community regularly this is obvious, but as someone who doesn't it wasn't. In fact I did ask a comment on that question, and yet 16 hours later it's still unanswered. It took seconds for this question to be answered. When I googled "python classes" I was taken to http://docs.python.org/2/tutorial/classes.html where there is not one example of "new-style" classes deriving from "object", which led me to wonder if I should declare my classes like `class MyClass` or `class MyClass(object)` hence my question. – User Dec 11 '12 at 21:42
  • 2
    This is not a duplicate: for starters it has the syntax for the two styles front and centre, which the linked question doesn't mention in either the question or the answer. This question / answer is much better for some someone new to python, or new to 2.x after only having used 3.x – Zero Mar 02 '16 at 23:39

2 Answers2

26

Always subclass "object". Those are new style classes.

  • You are ready for Python 3 that way.

  • Things like .super() work properly that way, should you need them.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Reinout van Rees
  • 13,486
  • 2
  • 36
  • 68
  • 6
    Not to mention the method decorators (`@property`, `@classmethod` and `@staticmethod`) only work with new-style classes. – Martijn Pieters Dec 11 '12 at 09:16
  • 3
    Can't you do `class MyClass:` in py3 and it's still a new style class though?? – GP89 Dec 11 '12 at 09:35
  • @GP89: Yes, in Python 3. – Lennart Regebro Dec 11 '12 at 22:32
  • @MartijnPieters could you, please, elaborate on that? I notice the answer is very old. I have recently tried to use those decorators on old style classes in python 2.7.14 and they seemed to work fine. – Bogdan Prădatu Feb 19 '22 at 21:07
  • 1
    @BogdanPrădatu they will not work fine. e.g. properties can’t have setters in old-style classes. The documentation explicitly states these only work on new-style classes; see the [property description](https://docs.python.org/2/library/functions.html#property) for example. – Martijn Pieters Feb 20 '22 at 22:07
11

You should always use new style classes. New-style classes are part of an effort to unify built-in types and user-defined classes in the Python programming language.

New style classes have several things to offer such as:

  • Properties: Attributes that are defined by get/set methods
  • Static methods and class methods
  • The new getattribute hook, which, unlike getattr, is called for every attribute access, not just when the attribute can’t be found in the instance
  • Descriptors: A protocol to define the behavior of attribute access through objects
  • Overriding the constructor new
  • Metaclasses

Source.

NlightNFotis
  • 9,559
  • 5
  • 43
  • 66