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

Charles-François Natali cf.natali at gmail.com
Fri May 3 08:56:37 CEST 2013


> First step: drop the function call
>
> stat_result.isfile() or stat_result.isdir() don't have to be functions.
> The feature can also be implemented with properties, e.g.
> stat_result.is_file. Or can somebody think of a reason why they have to
> be callables anymore?

Callables sound more consistent.

> Second step: get file type as string
>
> A property stat_result.file_type that returns the type of the file as
> string makes checks like "s.is_dir or s.is_file" even easier:
>
> s = os.stat(f)
> if s.file_type in {'reg', 'dir'}:
>    do_something()

Strings shouldn't be used for anything except text.
It defeats the typing system, prevents static check, offers poor
performance, etc.
This kind of attribute should ideally be an enum ;-)

Note that you have to be careful when changing os.stat() return type:
we absolutely don't want to break backward compatibility: for example,
the returned object should look like a tuple (among other things,
support indexing).



More information about the Python-ideas mailing list