Why is sys.path a list?
Why did the developers decide to make sys.path
in a list rather than an ordered set?
Having sys.path
as a list results in the possibility of having multiple paths in the path, slowing down the lookup times for modules.
An artificial example is 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
Summarizing from comments and responses:
From the answers below, it can be seen that a list is a simple structure that handles 99% of all needs, but does not have a safety feature to avoid duplicates, however, it does have a primitive prioritization which is the index of an item in the list where you can easily set the highest priority by adding or lowest priority by adding.
Adding earlier prioritization, i.e. insertion, before this element will rarely be used, as the interface to this would be too much of an effort for a simple task. As the accepted answer states, there is no practical need for anything more advanced covering these additional use cases as people are used to it.
source to share
- Ordered set
- a very recent idea (recipe mentioned in Does Python have an ordered set? for 2.6+)
- very special structure (even in the standard library)
- There is no practical need for additional complexity
- List is a very simple structure and ordered set is hash table + list + weaving logic
- You do not need to perform operations with
sys.path
for which the set is configured - check if the exact path is insys.path
- moreover, do it very quickly - In contrast,
sys.path
typical use cases are those specifically for a list: trying sequences, adding or adding one
To summarize, there is both a historical reason and a lack of any practical need.
source to share
sys.path
indicates the search path. Typically, search paths are ordered with the order of the elements indicating the search order. If there sys.path
was set
, then there would be no explicit ordering, making it sys.path
less useful. It should also be borne in mind that optimization is not an easy task. A smart optimization to deal with any performance issue is to simply keep a record of the items already found sys.path
. Trying to be complex with ordered sets is probably not worth the effort.
source to share