[Python-ideas] Better stdlib support for Path objects

Yury Selivanov yselivanov.ml at gmail.com
Mon Oct 6 22:00:36 CEST 2014


On 2014-10-06, 3:19 PM, MRAB wrote:
> On 2014-10-06 19:33, Barry Warsaw wrote:
>> On Oct 06, 2014, at 11:29 AM, Guido van Rossum wrote:
>>
>>> Sounds like a plan. I'd like to see how hard it is in practice to
>>> do the polymorphic return value thing.
>>
>> I'll do a limited experiment and post the results.
>>
> I wonder whether it might be cleaner to use a simple function for it,
> something like:
>
> def pathify(result_path, like=original_path):
>     if isinstance(original_path, Path):
>         return Path(result_path)
>
>     if isinstance(original_path, str):
>         return str(result_path)
>
>     raise TypeError('original path must be a path or a string, not 
> {!r}'.format(type(original_path)))

It's also possible to make a decorator for this, something along the 
lines of:

    @pathify(args=['path1', 'path2'], return_value=True)
    def method(path1, path2, option1):
        ...

Yury


More information about the Python-ideas mailing list