[issue11698] Improve repr for structseq objects to show named, but unindexed fields

Serhiy Storchaka report at bugs.python.org
Mon Oct 19 04:47:32 EDT 2020


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

Other problem is that the repr looks like an evaluable expression, but evaluating it will always produce error.

>>> st = os.stat('/dev/null')
>>> st
os.stat_result(st_mode=8630, st_ino=6, st_dev=6, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1602523313, st_mtime=1602523313, st_ctime=1602523313)
>>> os.stat_result(st_mode=8630, st_ino=6, st_dev=6, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1602523313, st_mtime=1602523313, st_ctime=1602523313)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: structseq() takes at most 2 keyword arguments (10 given)

os.stat_result() accepts only two arguments: a tuple for indexable elements and a dict for non-indexable elements.

>>> os.stat_result((8630, 6, 6, 1, 0, 0, 0, 1602523313, 1602523313, 1602523313), {'st_atime': 1602523313.282834, 'st_mtime': 1602523313.282834, 'st_ctime': 1602523313.282834, 'st_atime_ns': 1602523313282834115, 'st_mtime_ns': 1602523313282834115, 'st_ctime_ns': 1602523313282834115, 'st_blksize': 4096, 'st_blocks': 0, 'st_rdev': 259})
os.stat_result(st_mode=8630, st_ino=6, st_dev=6, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1602523313, st_mtime=1602523313, st_ctime=1602523313)

But such form looks not very readable, because it lacks names for indexable elements.

To solve this we can use an angular form in the repr:

<os.stat_result ...>

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue11698>
_______________________________________


More information about the Python-bugs-list mailing list