Hi,
I propose deprecating os.stat(filename)[8] tuple API to migrate to os.stat(filename).st_mtime named attributes. The tuple API exists for backward compatibility with Python 2.1 and older which was released 25 years ago.
The tuple API requires a good memory to remind members by their indexes. What are these members?
os.uname()[4]pwd.getpwnam('root')[3]sys.float_info[9]sys.flags[14]
Answers:
os.uname().machinepwd.getpwnam('root').pw_gidsys.float_info.radixsys.flags.utf8_mode
In the early days of Python, it was tedious to create an object with attributes in C. So instead, functions returned tuples, since it’s simple to create tuples in C. For example, st = os.stat() returned a tuple of 10 entries (integers) in Python 1.5. The stat provides indices to stat members: st[stat.ST_INO] returns the inode number.
Python 2.2 introduced a new C type structseq type to add attributes to these tuples. The new types inherit from tuple and so remains fully backward compatible. For example, st[stat.ST_INO] is still accepted, but you can now also write st.st_ino instead (the stat module is no longer needed) which is more readable (no need to remind what is st[1]).
Python uses structseq subclasses in many modules: grp, os, resource, signal, etc. Last years, new structseq types have been added. The new types also provide the tuple API, even if they are new and so don’t need backward compatibility. It’s just because it’s currently not possible to select if the tuple API is supported or not.
The os.stat_result type is even more complex because it has “unnamed members”. For example, st[8] (or st[stat.ST_MTIME]) is the modification time in seconds as an integer and it has no corresponding attribute, whereas st.st_mtime is the modification time in seconds as a float and it has not corresponding tuple index (st[11] raises IndexError).
IMO it’s now time to get rid of the backward compatibility with Python 2.1 (released in 2001, 25 years ago) and older. It’s time to migrate code to named attributes: replace st[8] with st.st_mtime.
The good news is that code modified to use named attributes would be compatible with Python 2.2 and newer!
I expect that the majority of code already use named attributes, so no code changed is needed, and that the majority of developers don’t even know that a tuple API exist.
I propose deprecating the tuple API (object[index] and len(object)) in most structseq objects. Examples: os.stat_result, grp.struct_group, sys.float_info, resource.struct_rusage.
The tuple API still makes sense in 2026 for some structseq objects which would be left unchanged: sys.version_info, curses.ncurses_version and time.struct_time. For example, sys.version_info[:2] is commonly used to get (major, minor) version.
Victor