Python: What is the use case for set.pop ()?

The built-in Python type sethas the pop () method from the docs:

Delete and return an arbitrary element from the set. Raises KeyError if the set is empty.

I could not come up with any precedent for this function, it seems to be an attempt to implement the interface list.

Why is this part of the Python standard?

+4
source share
1 answer

You would use this when you have a pool of jobs to process in a specific order. Tasks should be performed only once, but after it can be added again:

jobs = set([job1, job2, job3])

while jobs:
    job = jobs.pop()
    job.process()

job.process() . , .

, , . DependencyFinder.find() distlib ( pip, ), todo , .

dict.popitem(), :

popitem() , .

, set, None .

+7
source

All Articles