DIR ATTRIBUTES PYTHON 2.7 VS PYTHON 3.6

Words
111
Reading
1 min
Listen
Play
8y

Python 2.7:

 

Python 3.6:

Differences:

As we can see class A has more available attributes in Python3. Six of them are named “rich comparision”(‘__ne__’, ‘__ge__’, ‘__lt__’, ‘__gt__’, ‘__le__’, ‘__eq__’). To compare object instances you have to override above methods, otherwise TypeError will be raised:

> a <= b 

Traceback (most recent call last):

 File "python", line 1, in <module>

TypeError: '<=' not supported between instances of 'A' and 'A'

However in Python 2.7 the behaviour is different. It doesn’t raise any error.  Of course we can override  above methods and it will behave like we want.

__dir__  - return the list of attributes

__init_subclass__ - allows to customize subclass creation without using a metaclass 

DIR ATTRIBUTES PYTHON 2.7 VS PYTHON 3.6 | Ecency