Posts: 4,653
Threads: 1,496
Joined: Sep 2016
mutable sequences have a method called pop that can be optionally given one argument to use as the index, else 0 is used. the indexed element of the mutable sequence is the return value and is removed from the sequence.
my suggestion is to make it work much like the same name method of a mapping/dictionary. allow a 2nd argument which will be the default value if the sequence is too short to be indexed by the given index. by using a 2nd argument, no exception is raised, and the 2nd argument, the default value, is returned, when the index value is >= the length of the sequence.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 12,039
Threads: 487
Joined: Sep 2016
Whenever you feel strongly about a change to the core language, there is a process,
and you may be aware of this, but I will post the software foundation link for others
who may not: https://docs.python.org/devguide/langchanges.html
Posts: 2,128
Threads: 11
Joined: May 2017
data = [1, 2, 3, 4, 5]
while data:
print(data.pop(2, None)) I think with your improvement the loop will be infinite.
Suppress a IndexError is never a good idea in my opinion.
But this have to be discussed :-)
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
@ DeaD_EyE i agree, that would loop infinitely. that would not be the right way to use the feature.
i have a few times run into the case where this feature would have made what i think is nicer code. but i coded it with a length test instead, much like i would code an "in" test for a dictionary if its pop method did not allow the 2nd argument.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 3,458
Threads: 101
Joined: Sep 2016
So it's kind of like the dict's .get() method, except it also .pop()'s the index?
What about something like this, wrapped up in a module so NoDefault is namespaced correctly?
>>> NoDefault = (None, )
>>> class DefaultList(list):
... def _pop_default(self, index=0, default_value=NoDefault):
... try:
... value = self._pop_original(index)
... return value
... except IndexError as err:
... if default_value is not NoDefault:
... return default_value
... raise err
... def __init__(self, *args, **kwargs):
... super().__init__(*args, **kwargs)
... self._pop_original = self.pop
... self.pop = self._pop_default
...
>>> items = DefaultList(["spam", "eggs", "foo", "bar"])
>>> items
['spam', 'eggs', 'foo', 'bar']
>>> items[3]
'bar'
>>> items.pop()
'spam'
>>> items
['eggs', 'foo', 'bar']
>>> items.pop(1)
'foo'
>>> items
['eggs', 'bar']
>>> items.pop(5, "default")
'default'
>>> items.pop(5, None)
>>> items.pop(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in _default_pop
File "<stdin>", line 4, in _default_pop
IndexError: pop index out of range
>>> items
['eggs', 'bar']
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
dict has .pop(index) and .pop(index,default)
list only has .pop(index) my suggestion is to add .pop(index,default)
but is it worth having? i have run into cases where i almost coded it because i often use dict.pop(index,default). otoh, working around the lack of it was not as hard as it would be if dict didn't have. so i don't know.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,128
Threads: 11
Joined: May 2017
When you can prove it with existing projects, then a PEP should be written.
This makes also a unification of the methods.
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
if i had designed a language with the first 3 features i would have just included the 4th under my personal philosophy of "filling in the holes". but that's me. and that's not a basis for PEPs. i didn't know if others had run into such cases of need or not. so i asked.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
|