Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Path or PurePath
#1
i am writing a function that may be given a pathlib Path or PurePath object, or a string, or bytes, or bytearray. the first two are what concerns me. how can i test what it is that the function is getting when called? will a test for Path be True if what i got was really a PurePath? or the reverse? or are they distinctive enough for isinstance() to tell them apart? i don't know,yet, if i do need to tell them apart. this function will be a file tree recursion generator yield Path objects of all types found in the tree. i have already done such a function that yields strings. i want to make this more useable.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Path() inherits from PurePath(), so yes, if you test with isinstance() you will get uncertain results
from pathlib import PurePath, Path

p = Path()
pp = PurePath()
print(isinstance(p, PurePath))
print(isinstance(p, Path))
print(isinstance(pp, PurePath))
print(isinstance(pp, Path))
Output:
True True True False
so, if you want to distinguish this is one of the cases when you may want to use type() instead of isinstance() OR you need to use complex if condition.

Do you need to distinguish - sounds like NO to me, but probably it depends how you will implement the function.
Quote: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.
The difference is the I/O operations, but you can always yield Path anyways:

if isinstance(p, (Path, PurePath)):
    yield Path(p)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
maybe not. i'm not sure, yet. but i realized that the way these are related, i could run into complications.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,150 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  PurePath.relative_to() fails in 3.6 Skaperen 5 3,479 Oct-18-2019, 06:28 AM
Last Post: DeaD_EyE
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,671 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908
  getting a full path string from a pathlib.PurePath object Skaperen 14 140,524 Mar-24-2018, 03:55 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020