Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Default Inheritance?

If I define a class in Python such as:

class AClass:
   __slots__ = ['a', 'b', 'c']

Which class does it inherit from? It doesn't seem to inherit from object.

like image 823
Verhogen Avatar asked Feb 05 '11 09:02

Verhogen


1 Answers

Try the following code snippet in Python 2.7 and Python 3.1

class AClass:
   __slots__ = ['a', 'b', 'c']
print(type(AClass))
print(issubclass(AClass,object))
print(isinstance(AClass,type))

In Python 2.7, you will get:

<type 'classobj'>
False
False

And Python 3.1 you will get.

<class type>
True
True

And that explains it all. It is old style class in Python 2, unless you subclass it from object. Only in Python3, it will be treated like a new style class by default.

like image 123
Senthil Kumaran Avatar answered Sep 27 '22 23:09

Senthil Kumaran