Sorting your custom class instances in Python

preview_player
Показать описание
Did you just get a TypeError with something not supported between instances of 'X' and ‘Y' in Python? Learn what the error means and how to fix it, so your class instances can be sorted by Python.

C H A P T E R S
0:00 TypeError when sorting objects
0:40 Sorting a regular list of names algorithm in Python
1:40 Timsorting
2:00 Implement dunder less then (__lt__)

M Y O N L I N E T R A I N I N G

L I N K S I N T H E V I D E O

#pythonforeveryone #programming #python
Рекомендации по теме
Комментарии
Автор

I did a bit of digging, because had no idea why did Python returned False and True values, based on what I've been doing with string or if greater or less sign was used in the statement.

In short summary, Python uses lexicographical ordering, which means it evaluates each character in a given string using Unicode code point number and then compares it with the character with corresponding place in the other string.

Took some testing and found out for example that comparing strings: AAC < AABA gives False value. Meaning that as soon as Python finds that one of the string has greater value, it assumes it is so, no matter what characters are given after reaching the conclusion.

Zgl
Автор

lt short from less than, so how about more than?

albertarysoesilo
join shbcf.ru