#πŸ”’ Returning bool by checking a type

36 messages Β· Page 1 of 1 (latest)

latent forge
#

Currently my code checks if a class type corresponds to the correct class by changing it to a string and checking that the type returned as a string corresponds to the string that ive been given like the photo attached. Is there another way I can do this

supple turretBOT
#

@latent forge

Python help channel opened

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.

bitter cliff
#

!d isinstance

supple turretBOT
#

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).
bitter cliff
#

this?

vital agate
hardy ferry
#
return type(self.store) is RouteSeries
hardy ferry
coral badger
trim heath
#

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()}
willow lily
willow lily
#

checking for class name seems like a very bad practice

trim heath
#

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

willow lily
#

then a solution based on inspect.getsource would be way more reliable imo

#

but that's like killing a fly with a tank

trim heath
#
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

willow lily
#

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

trim heath
#

I encountered the second problem and reached for checking the name

willow lily
#

anyway, in the absolute majority of usecases, checking type is enough

trim heath
#

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

willow lily
#

that's very specific to you though

trim heath
#

Not saying it isnt

willow lily
#

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

trim heath
#

Doesn't seem likely so far

supple turretBOT
#
Python help channel closed

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.