Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is sys.path a list?

Tags:

Why would the implementers choose to make sys.path into a list as opposed to a ordered set?

Having sys.path as a list gives rise to the possibility of having multiple duplicates in the path, slowing down the search time for modules.

An artificial example would be the following silly example

# instant importing
import os
import sys

for i in xrange(50000):
    sys.path.insert(0, os.path.abspath(".")

# importing takes a while to fail
import hello

To summarise from the comments and answers given:

It seems from the responses below that a list is a simple structure which handles 99% of everyone's needs, it does not come with a safety feature of avoiding duplicates however it does come with a primitive prioritisation which is the index of the element in the list where you can easily set the highest priority by prepending or lowest priority by appending.

Adding a richer prioritisation i.e. insert before this element would be rarely used as the interface to this would be too much effort for a simple task. As the accepted answer states, there is no practical need for anything more advanced covering these extra use cases as historically people are used to this.

like image 935
Har Avatar asked Apr 25 '17 15:04

Har


2 Answers

  • Ordered set is
    • a very recent idea (the recipe mentioned in Does Python have an ordered set? is for 2.6+)
    • a very special-purpose structure (not even in the standard library)
  • There's no practical need for the added complexity
    • List is a very simple structure, while ordered set is basically a hash table + list + weaving logic
    • You don't need to do operations with sys.path that a set is designed for - check if the exact path is in sys.path - even less so, do it very quickly
    • On the contrary, sys.path's typical use cases are those exactly for a list: trying elements in sequence, prepending or appending one

To summarize, there's both a historical reason and a lack of any practical need.

like image 164
ivan_pozdeev Avatar answered Sep 24 '22 10:09

ivan_pozdeev


sys.path specifies a search path. Typically search paths are ordered with the order of the items indicating search order. If sys.path was a set then there would be no explicit ordering making sys.path less useful. It's also worth considering that optimization is a tricky issue. A reasonable optimization to address any performance concerns would be to simply keep a record of already searched elements of sys.path. Trying to be tricky with ordered sets probably isn't worth the effort.

like image 41
John Percival Hackworth Avatar answered Sep 25 '22 10:09

John Percival Hackworth