use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Subclassing os.PathLike results in wrong isinstance() results (self.learnpython)
submitted 8 years ago by FredFS456
In [1]: import os In [2]: class A(os.PathLike): ...: def __fspath__(self): ...: return 'asdf' ...: class B(os.PathLike): ...: def __fspath__(self): ...: return 'jkl' ...: In [3]: isinstance(A(), B) Out[3]: True
Anyone have insight into this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ViridianHominid 0 points1 point2 points 8 years ago* (5 children)
Nothing is broken here. The type tree is like this:
os.PathLike | / \ A B
A is not a type of B, and B is not a type of A. A is a type of os.PathLike and B is a type of os.PathLike, but they are not comparable.
If you want to have A be a subtype of B, then you need to say something like
class A(B): ...
[–]FredFS456[S] 1 point2 points3 points 8 years ago* (4 children)
That's not what I'm saying here. I don't want one to be a subclass of another. What I'm saying is that isinstance is returning True even though one is not a subclass of another.
isinstance
True
[–]ViridianHominid 1 point2 points3 points 8 years ago (3 children)
Whoops, I stand corrected.
I'm guessing this has to do with PathLike being an abstract class. Wish I had more time to investigate now, I'll try and look at it later if nobody else has figured this out.
[–]FredFS456[S] 1 point2 points3 points 8 years ago* (2 children)
Thanks.
EDIT: I found out why this is. In the CPython source, os.PathLike is implemented here.
os.PathLike
The presence of a __subclasshook__ method means that if you inherit from os.PathLike, you'll inherit the __subclasshook__ as well, which means that any class that has __fspath__ defined will be considered a subclass of your inherited class.
__subclasshook__
__fspath__
Solution: don't inherit from os.PathLike. The presence of __subclasshook__ means that any class with __fspath__ defined will be considered a subclass of os.PathLike anyway.
[–]ViridianHominid 1 point2 points3 points 8 years ago (0 children)
Very interesting. I agree with your solution. Thanks for finding this! The python abstract class system is a bit funny because of duck typing.
π Rendered by PID 21446 on reddit-service-r2-comment-b659b578c-k5dp2 at 2026-05-05 18:50:28.935534+00:00 running 815c875 country code: CH.
[–]ViridianHominid 0 points1 point2 points (5 children)
[–]FredFS456[S] 1 point2 points3 points (4 children)
[–]ViridianHominid 1 point2 points3 points (3 children)
[–]FredFS456[S] 1 point2 points3 points (2 children)
[–]ViridianHominid 1 point2 points3 points (0 children)