Python Forum
a suggestion for a feature change
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a suggestion for a feature change
#1
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.
Reply
#2
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
Reply
#3
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 :-)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
@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.
Reply
#5
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']
Reply
#6
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.
Reply
#7
When you can prove it with existing projects, then a PEP should be written.
This makes also a unification of the methods.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IDE suggestion JackMack118 5 3,046 Nov-21-2019, 11:35 PM
Last Post: JackMack118
  Need Suggestion for a Good Python IDE Aprogrammer 6 3,828 Aug-14-2018, 02:22 PM
Last Post: Aprogrammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020