Nov-15-2017, 10:13 PM
Please look into the source code. Github makes it very easy for us.
The bult-in function
So we should look into the pathlib module and seek for __lt__
It's hard to follow the code, because there is heavy inheritance and some other patterns.
You should know that pathobject._cparts returns a list spitted by it's parts. A path separator is also a part, if it's the root directory (absolute path).
Comparing lists in Python does not do what you think it should do. It compares in lexicographical order. I got this answer from here.
Comparing the paths as str:
The bult-in function
sorted
calls __lt__ (means less then) of an object.So we should look into the pathlib module and seek for __lt__
It's hard to follow the code, because there is heavy inheritance and some other patterns.
You should know that pathobject._cparts returns a list spitted by it's parts. A path separator is also a part, if it's the root directory (absolute path).
Comparing lists in Python does not do what you think it should do. It compares in lexicographical order. I got this answer from here.
Comparing the paths as str:
'/spam/spams.txt' < '/spam/spams/spam04.doc'
Output:True
Comparing the _cparts:['/', 'spam', 'spams.txt'] < ['/', 'spam', 'spams', 'spam04.doc']
Output:False
Index 0 and 1 are identical. Index 3 is bigger than index 3 from the other list. Then there are no more elements in the first list for comparison and the operation stops and yields the last result, which is False
.'spams.txt' < 'spam04.doc'In simple words: Paths are sorted in alphabetical order by their elements.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!