I’m working on a Python project initially coded with path.py 16.4
, where it was possible to create an empty Path object with Path()
. After migrating to pathlib
, this is no longer possible as Path()
now returns the current directory (.
).
My goal is to create a separate class that:
- Allows creating an empty Path object
- Returns
''
(empty string) - Has a boolean evaluation of
False
- Allows using
Path
methods - Doesn’t modify the behavior of other
Path
instances (their boolean evaluation should remain unchanged)
I’m considering three approaches and would like the community’s opinion to understand and make the right choice:
- Composition:
from pathlib import Path
class EmptyPath:
def __init__(self):
self._path = Path()```
2. Inheritance
from pathlib import Path
class EmptyPath(Path):
_flavour = type(Path())._flavour
def new(cls, *args, **kwargs):
self = super().new(cls, ”)
return self
3. Metaclass
from pathlib import Path
class EmptyPath(type(Path())):
def new(cls, *args, **kwargs):
return super().new(cls, ”)“
Which approach would be most appropriate to create an EmptyPath Class comptaible with pathlib?
Are there specific advantages or disadvantages to each method in this context?
Thank you in advance for your advice and explanations!