Old-style and new-style classes in Python 2.7 [duplicate]
Asked Answered
R

2

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
Ridglee answered 11/12, 2012 at 9:4 Comment(6)
@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.Ridglee
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.Kishakishinev
@LennartRegebro: Yes 4 years is a lot of time, a lot can change. Didn't realize Python 2.6 was 4 years ago.Ridglee
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.Kishakishinev
@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 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.Ridglee
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.xGlomma
C
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.

Christadelphian answered 11/12, 2012 at 9:7 Comment(5)
Not to mention the method decorators (@property, @classmethod and @staticmethod) only work with new-style classes.Martins
Can't you do class MyClass: in py3 and it's still a new style class though??Bencion
@GP89: Yes, in Python 3.Kishakishinev
@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.Oryx
@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 for example.Martins
V
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.

Voncile answered 11/12, 2012 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.