[Python-ideas] Extend the os.stat() result objects with methods like isfile() and isdir()

Andrew Barnert abarnert at yahoo.com
Fri May 3 18:06:33 CEST 2013


On May 3, 2013, at 8:14, Pieter Nagel <pieter at nagel.co.za> wrote:

> To all the proponents of a file_type() attribute: can you please show
> some use-cases for this?

There's one really obvious one for os.walk or similar functions:

types_to_recurse = ('dir', 'link') if follow else ('dir',)
# ..,
if s.file_type in types_to_recurse:
    try_to_recurse()

Meanwhile, it strikes me that if you just change this to is_type(types_to_check), it gives a parallel with isinstance and friends, which has two benefits.

First, it means you can handle synonyms. Just like isinstance can handle subclasses/ABC registration/etc. while type() cannot, istype can handle both 'reg' and 'file' as the same type while file_type cannot.

Second, by following the usual python pattern for pre-checking, it makes it blindingly obvious that you're violating EAFTP, forcing you to think about whether you have a good reason to do so. In the example above, I do (I don't want to try recursing into symlinks if follow is false, even though it would work), but that may not be true for every use case.


> I don't want to complicate the PEP just for some speculative
> nice-to-have.
> 
> -- 
> Pieter Nagel
> 
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



More information about the Python-ideas mailing list