Why in class declarations in Python should we extend object?

12

No Python , when we declare a class, we extend object .

class StackExchange(object):
    def __init__(self):
        pass

I do not know if I'm wrong, but I had the impression that in some versions this is mandatory; and not in others.

Why should we pass object as a parameter? Is this part of any convention?

    
asked by anonymous 04.03.2015 / 21:34

2 answers

10

First you are not passing any parameters, the syntax seems to refer to this but it is just the Python way of indicating which class you are inheriting.

There has been a change in the model that the language implements the hierarchies at a given time (Python 2.2 and 2.3) called " new style object-oriented. " In this way Python has adopted a model already adopted by some other languages, in which all objects must inherit from a single root that guarantees the existence of certain members in all the objects. These members exist in class object , so this class must always be inherited directly or indirectly - when inheriting from another class that is already inheriting object .

This form unifies typing, which is a great facilitator treating built-in types evenly.

Some languages even do this automatically, but apart from requiring compatibility as an old or classic style, the pythonic way of coding is being explicit, so it is consistent to have to declare inheritance in every case.

Of course, it's still possible to use the old style, but several newer features will not work because they expect to have these members in the classes.

In Python 3.x only the new style should be used. In general, there are no more non-declaration problems in object , since it has become implied. However, it is recommended that you do so not only to improve readability, but to facilitate a downgrade of used code.

The change brought super() , the describers and __slots__ . In addition there was a change in the way method resolution .

    
04.03.2015 / 21:45
11

This is not required, but it is recommended, and the behavior is different in Python 2 and 3.

According to this documentation , until version 2.1 class concept ( class ) was different from the type concept ( type ), so that x.__class__ returned the class of x but type(x) always returned <type 'instance'> . From 2.2 these concepts have been unified, so a new-style class is simply a user-defined type, and both x.__class__ and type(x) return the same thing * - a class of x . The reason cited was:

  

The greatest motivation to introduce new-style classes is to promote the unification of an object model with a complete meta-model. It also has some practical benefits, such as the ability to create subclasses of most built-in types, or the introduction of descriptors , which allow computed properties. >

For compatibility, the "old-style" classes continued to be supported in Python 2, and the default when using:

class Foo:

is the creation of an old-style class, while using:

class Foo(object):

If you create a new-style class that inherits explicitly from object . There is little reason to create old-style classes (and some benefits in using the other), so the second syntax is always recommended.

In Python 3, there are no old-style classes, all classes are new-style by default. So either way if you use either syntax, the result will be the same. But for the sake of consistency, so to speak, it is recommended that you still use the second syntax - which makes it more explicit of which other class (s) this new class is inheriting.

Some practical differences between the old-style and new-style classes:

  • The new ones allow the use of super , to access members of the superclasses directly;
  • The new ones allow the use of __slots__ , when you do not want an "open" class - that accepts new fields or which fields are deleted - but one with well defined and fixed fields;
  • New ones can not be released, unless they inherit from Exception ;
  • The order of method resolution is different, when there is multiple inheritance involved (i.e. which superclass is visited first, when the method does not exist in the class itself).

Font

* Unless of course the class / object redefines __class__ , but that's another story ...

    
04.03.2015 / 21:47