#π Returning bool by checking a type
36 messages Β· Page 1 of 1 (latest)
@latent forge
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!d isinstance
isinstance(object, classinfo)```
Return `True` if the *object* argument is an instance of the *classinfo* argument, or of a (direct, indirect, or [virtual](https://docs.python.org/3/glossary.html#term-abstract-base-class)) subclass thereof. If *object* is not an object of the given type, the function always returns `False`. If *classinfo* is a tuple of type objects (or recursively, other such tuples) or a [Union Type](https://docs.python.org/3/library/stdtypes.html#types-union) of multiple types, return `True` if *object* is an instance of any of the types. If *classinfo* is not a type or tuple of types and such tuples, a [`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError) exception is raised. [`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError) may not be raised for an invalid type if an earlier check succeeds.
Changed in version 3.10: *classinfo* can be a [Union Type](https://docs.python.org/3/library/stdtypes.html#types-union).
this?
instead of str you may want repr() but isistance should be better to use
return type(self.store) is RouteSeries
this checks exact class. it doesn't check for subclasses
to also include subclasses use isinstanceof()
ty
That should have been isinstance()
It will not work if you import the same class from different modules
What I do
def is_subclass(cls, class_name):
return class_name.__name__ in {c.__name__ for c in cls.mro()}
== also suffice
then are they really the same though ?
checking for class name seems like a very bad practice
Sorry, my bad. I meant the same class from the same module, in different ways
Like, absolute vs relative import
The "module" part of the class will be different, and the classes are not equal
Even though you import from the same place
then a solution based on inspect.getsource would be way more reliable imo
but that's like killing a fly with a tank
An OSError is raised if the source code cannot be retrieved. A TypeError is raised if the object is a built-in module, class, or function.
I'll just check the name :)
Seems like a pythonic solution to me
heh, if you have 2 very different classes having the same name from 2 different packages this method could return True though
which is way more plausible to happen than the same class from the same package being imported twice differently
I encountered the second problem and reached for checking the name
it wouldn't be built-in so it would work
anyway, in the absolute majority of usecases, checking type is enough
I needed to check if an error is a subclass of some type (I use custom types RetryableError and NonRetryableError as mixins), and only this worked
that's very specific to you though
Not saying it isnt
the better method would be to have a ref storage in the class that stores a reference to all the objects created from it and then check identity on those
I'll do that if this bites me
Doesn't seem likely so far
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.