This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add .lastitem attribute to takewhile instances
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Claudiu.Popa, oscarbenjamin, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-08-23 12:19 by oscarbenjamin, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
itertools_takewhile.patch Claudiu.Popa, 2013-09-07 15:30 review
Messages (5)
msg195962 - (view) Author: Oscar Benjamin (oscarbenjamin) * Date: 2013-08-23 12:19
I've often wanted to be able to query a takewhile object to discover the item that failed the predicate but the item is currently discarded.

A usage example:

def sum(items):
    it = iter(items)
    ints = takewhile(Integral.__instancecheck__, it)
    subtotal = sum(ints)
    if not hasattr(ints.lastitem):
        return subtotal
    floats = takewhile(float.__instancecheck__, it)
    subtotalf = fsum(floats)
    if not hasattr(floats.lastitem):
        return subtotal + subtotalf
    # Deal with more types

Loosely what I'm thinking is this but perhaps with different attribute names:

class takewhile(pred, iterable):
    def __init__(self):
        self.pred = pred
        self.iterable = iterable
        self.failed = False
    def __iter__(self):
        for item in self.iterable:
            if self.pred(item):
                yield item
                self.failed = True
                self.lastitem = item
msg197167 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-09-07 15:30
Hello. Here's a basic patch with tests which accomplishes your request. Currently, it defaults to None if no item failed, but probably it can be set only when something fails the predicate (and the user will check using hasattr(t, 'last') ).
msg197171 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-07 16:24
Oscar, did you considered itertools.groupby()? Perhaps it better meets your needs.
msg197311 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-09-08 19:14
Oscar, the solution proposed by Serhiy looks like a better choice.

I'm wary of increasing the API complexity of the itertools.  Right now, their learnability is aided by having simple signatures and no side-values.

The itertools are modeled on functional tools in other languages with mature APIs.  I look to those languages to provide an indication of whether proposed features are needed in practice.  AFAICT, there is no precedent for a takewhile-with-failed-value combo.

I appreciate your request (especially because it was accompanied by a use case) but am going to decline.  IMO, the module as a whole is better served by keeping the tools simple and clean. 

If an individual itertool doesn't have an exact fit to a particular use case, it may indicate that the programmer would be better served by a simple generator which can express the logic more cleanly than a tricked-out itertool with side-values.
msg197317 - (view) Author: Oscar Benjamin (oscarbenjamin) * Date: 2013-09-08 20:16
Thank you Claudiu very much for writing a patch; I was expecting to
have to do that myself!

Serhiy, you're right groupby is a better fit for this. It does mean a
bit of reworking for the (more complicated) sum function I'm working
on but I've just checked with timeit and it performs very well using
the type function as a predicate. I think it might make the function a
few times faster than takewhile in my common cases for reasons that
are particular to this problem.

Raymond, thanks for taking the time to consider this. I agree that it
should now be closed.
Date User Action Args
2022-04-11 14:57:50adminsetgithub: 63021
2013-09-08 20:16:03oscarbenjaminsetmessages: + msg197317
2013-09-08 19:15:00rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg197311
2013-09-07 16:24:36serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg197171
2013-09-07 15:30:48Claudiu.Popasetfiles: + itertools_takewhile.patch

nosy: + Claudiu.Popa
messages: + msg197167

keywords: + patch
2013-09-01 06:23:52rhettingersetassignee: rhettinger
2013-08-23 16:15:45ned.deilysetnosy: + rhettinger
2013-08-23 12:19:54oscarbenjamincreate