Opened 22 months ago

Last modified 4 weeks ago

#26769 needs_info enhancement

Add an issequence utility to check for list, tuple, and other compatible objects — at Initial Version

Reported by: embray Owned by:
Priority: major Milestone: sage-9.2
Component: misc Keywords: python3
Cc: chapoton, jdemeyer, tscrim Merged in:
Authors: Erik Bray Reviewers:
Report Upstream: N/A Work issues:
Branch: u/embray/misc/issequence (Commits) Commit: 1d7066c898c4885ff33e5385b20d5892993cbc98
Dependencies: Stopgaps:

Description

Adds an issequence() function that can work as a more generic replacement for isinstance(x, (list, tuple)) but will also work with a broader range of similar types (e.g. xrange, Sage vectors, Numpy arrays, etc). Note issequence(x) is also True for str and bytes. So when using this to replace (list, tuple) care should be taken to make sure other sequence-like types are handled first, in cases where they require separate handling in the first place.

For the very common case of (list, tuple) this is faster than isinstance, but also has the benefit of being more generic, while not quite as generic as the much slower isinstance(x, Sequence).

Change History (0)

Note: See TracTickets for help on using tickets.