[Python-ideas] Enhanced stat_result objects (was: Better stdlib support for Path objects)

Paul Moore p.f.moore at gmail.com
Tue Oct 7 16:08:38 CEST 2014


On 6 October 2014 18:47, Barry Warsaw <barry at python.org> wrote:
> Over in issue 22570, I lament the fact that while pathlib is awesome, its
> wider support in the stdlib is pretty sparse.

The other thing that bugs me whenever I try to use pathlib is the
non-caching behaviour of the stat-related methods. I know why this is
as it is, and I agree with the reasons, but for some reason, I still
find that I expect code like

    p = Path(...)
    if not p.exists():
        print("Nonexistent")
    elif p.is_dir():
        print("Directory")
    elif p.is_file():
        print("File")
    else:
        print("Something else")

to only call stat once - even though I *don't* expect that of the
equivalent os.path based code.

I would find it really useful if stat_result objects supported the
various is_XXX methods from pathlib. Then I could write code like:

    p = Path(...)
    st = p.stat()
    if st.exists():
        ...
    elif st.is_dir():
        ...

which explicitly shows that stat is only called once, but doesn't
involve verbose and ugly code like

    if stat.S_ISDIR(st.st_mode):
        ...

Would this be a worthwhile addition?
Paul


More information about the Python-ideas mailing list