[Python-ideas] Add "default" kwarg to list.pop()

Antoine Pitrou solipsis at pitrou.net
Wed Oct 31 07:07:10 EDT 2018


On Wed, 31 Oct 2018 02:25:25 +0200
Serhiy Storchaka <storchaka at gmail.com>
wrote:
> 31.10.18 01:44, Giampaolo Rodola' пише:
> > Sorry in advance if this has been proposed in the past but I couldn't 
> > find anything on python-ideas:
> >   
> >  >>> l = []
> >  >>> l.pop(default=1)  
> > 1
> > 
> > FWIW my use case consists in reading entries from /proc/diskstats where 
> > lines can have a variable number of fields depending on the kernel version:
> > https://github.com/giampaolo/psutil/issues/1354#issuecomment-434495870
> > https://github.com/giampaolo/psutil/blob/d8b05151e65f9348aff9b58da977abd8cacb2127/psutil/_pslinux.py#L1068
> > As such it would be convenient to set missing fields to 0 as "reads = 
> > fields.pop(default=0)" instead of catching IndexError every time. Extra: 
> > for consistency set.pop() should probably have the same.
> > 
> > Thoughts?  
> 
> It is just
> 
>      l.pop() if l else default
> 
> or
> 
>      (l or [default]).pop()

l.pop(default=...) has the potential to be multi-thread-safe, while
your alternatives haven't.

Regards

Antoine.




More information about the Python-ideas mailing list