Class structure in Gramps: primary object

Creating a class using each syntax and checking their class and baseclass, both appear to be defined similarly, each being of class ‘type’ derived from class ‘object’:


>>> class newClass:
...     pass
...
>>> newClass.__class__
<class 'type'>
>>> newClass.__class__.__base__
<class 'object'>


>>> class objClass(object):
...     pass
...
>>> objClass.__class__
<class 'type'>
>>> objClass.__class__.__base__
<class 'object'>

BTW, I’m no Python expert - this is a learning experiment for me.

@codefarmer: thanks for experimenting

However, still according to the Python manual, I think inheritance is not the full story. It is clearly stated that the top class tree structure is rather “twisted”.

I think the main difference comes from object being a “featureless” (see manual) object and type is a metaclass (used to create classes instead of instances). Therefore object has class type (clearly shown by your example) and type has also class type because everything in Python must derive from some class. Both are Python objects (a container for the representation data) at top level which is confirmed by __base__ value.

Notation class xxx(object): was necessary under Python 2 to create “new style” classes which are now the default under Python 3. Thus (object) can be dropped without adverse effects.

But this is not the same for class yyy(type): which creates a metaclass while class yyy: creates an ordinary class. It can make a difference in some contexts.

Agreed! Using dir((yyy) and comparing to dir(xxx) showed quite a few differences. Not having used metaclasses in Python, this was a good discussion to follow.

Nope. If I read correctly the Python manual, class xxx: creates a class derived from type , i.e. it already inherits a certain number of methods. class xxx(object): creates a much poorer class with nearly no methods.

Sorry, but then you’re reading it wrong. They are equivalent.