1

Tried to rephrase my question to be less opinion based.

What are the downsides to pathlib.Path object coerce/evaluate to False when the filesystem path does not exist?

For example pathlib.Path could have been written with any __bool__ logic as it is just a Python class

class Path(object):
    def __init__(self, path):
        self.path_ = path

    def __bool__(self):
        return self.checkIfExistsOnFileSystemOrNot_()

    def path(self):
        return self.path_

    def checkIfExistsOnFileSystemOrNot_(self):
        # any logic here ...

    ...

I understand you might want to build up a filepath that is not "there" in a python program but when would you want to do

if path:
   # do something with the path "assuming" it exits on the filesystem (and not just that it's not falsy)

I'll grant that

if path.exists():
    # ...

Isn't that much more effort but given I've been jumping around between languages this has caused me some pain.

bpw1621
  • 2,962
  • 2
  • 32
  • 35
  • If `.exists` returned `None` if the file didn't exist, what would you want it to return if the file *did* exist? It seems very strange to have `None` / `True` as the return options. If was `None` / file_path, then I could understand it. But `.exists` is asking a very specific, boolean question. So a boolean output seems natural in this case. – Dan Feb 17 '20 at 22:17
  • .exists returns True/False, not None – bpw1621 Feb 17 '20 at 22:19
  • Oh or are you asking why the entire object `Path("non-existent-path")` isn't `None` instead of a Path object? In that case it is because pathlib allows you to work with paths that don't exist. This allows you to do things like create files at specific locations or work with posix paths whilst in Windows. – Dan Feb 17 '20 at 22:20
  • @Dan I mean when it's coerced/converted to a bool ... like in an if statement ... other languages don't behave this way – bpw1621 Feb 17 '20 at 22:23
  • The term for this is "[falsy](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false)" - i.e. when used as a condition, it behaves like `False`. – kaya3 Feb 17 '20 at 22:26
  • 1
    Because unlike a list or a string, which make sense to be Falsy when they are empty (not None as then they are no longer lists or strings), a Path object to a path that doesn't exist is still a useful, non-empty construct that you might need to work with. – Dan Feb 17 '20 at 22:26
  • @Dan what's an example of a useful coercion to True for a pathlib.Path that does not exist ... really just trying to wrap my head around it since, like I said, other language filesystem libraries behave differently. – bpw1621 Feb 17 '20 at 22:31
  • Building up paths for other file-systems aside from the one you are working in. For example if you're working on a Windows computer but need to create some paths to test inside a docker container running linux maybe. – Dan Feb 17 '20 at 22:44
  • Another reason is to create a file. I can create a path to a folder that doesn't exist. I might still want to perform path operations on the folder such as adding a subpath and then creating a file there. So in that case, I have an object that I can work with a perform operations on. It has state, it just so happens that the state it it representing doesn't exist yet. But it makes no sense to reduce it to a True or False because it has a non-empty state. Hence why it makes more sense to have a `.exists` to check if there is a file at that path. – Dan Feb 17 '20 at 22:44
  • @Dan not following sorry and we can take this offline at this point but in your 2 use-cases above when would you be coercing to bool? – bpw1621 Feb 17 '20 at 22:48
  • My point is that things in python are falsy when they are empty. A path that doesn't exist is not empty because it contains useful state information. Thus it doesn't make sense to coerce it to False, and hence why it does make idiomatic sense to have an exists. The path object is modelling a file system. If I create a country object and set its state to "Westeros" I wouldn't want `if my_country:` to return false just because the country I set it to is fictitious. That would be weird. But `if my_country.exists:` could be useful. And it is the same with fictitious (i.e. non-existent) file paths. – Dan Feb 17 '20 at 22:58
  • @Dan Okay wait sorry ... I think I see what my misunderstanding might have been ... in C++ an object can evaluate to false in a boolean context without being null/void ... I thought Python objects could as well but maybe that's my mistake – bpw1621 Feb 17 '20 at 23:06
  • 1
    You can make your objects [behave however you want in Python](https://docs.python.org/3/reference/datamodel.html#object.__bool__) in terms of coercing them to Truthy / Falsy states, it just seems weird to me to do this for a non-empty path object. – Dan Feb 17 '20 at 23:12

1 Answers1

2

Now that the question has been changed, the last point I made in my original answer is probably the most important: clarity.

The Zen of Python says:

Explicit is better than implicit.

and

Readability counts.

Explicitly calling .exists() on a Path object is very clear. A new programmer, unfamiliar with Python, could read

if some_path.exists():
    # ...

and probably understand it. But

if some_path:
    # ...

is much less clear.

I've been jumping around between languages this has caused me some pain.

Idiomatic Python and idiomatic Ruby or C# or Scala are all different. That's okay.

Now, to the rest of old answer:

pathlib isn't just for representing files and folders on the specific visible filesystem. It's for representing and manipulating paths per se (bold added):

This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.

You can even manipulate Windows-style paths on Linux systems and vice-versa.

Reducing the truthiness of paths to "does this file exist?" would seem like a strange design decision. Especially when there are other Boolean things that can be asked about files and directories: Is it readable? Writable? Executable? A symbolic link? A directory? A regular file? A FIFO?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 2
    _"respectfully disagree"_ this is why opinion based questions are a bad fit for SO. I can't see how an answer to this question could be more correct. – Dan Feb 17 '20 at 22:45