0

Reading documentation for isinstance() and looking up examples online, examples only illustrate how to use it with builtin classes like: str, int, float, long

I have code where the variable myObj.location will either be None if it hasn't been set yet, be an empty str if the function fails to return a value, or if it succeeds, then the variable will hold an object of type: geopy.location.Location

To test what this object holds, I could write code like this: isinstance(myObject.location, (type(None), str))

If False then I know it failed to return the Location object. If True then it either holds None or a str.

Is there a way to tell it directly to test if the object is geopy.location.Location returning True if the test is right?

My code works with the False test but I like the readability of being able to say True for an object of type geopy.location.Location that may or may not have been built for the first time at the moment of the isinstance test.

TMWP
  • 1,545
  • 1
  • 17
  • 31
  • What makes you think `isinstance` *doesn't* work with non-builtin classes? – user2357112 Jun 06 '18 at 16:34
  • What prevents you from using `isinstance(myObject.location, geopy.location.Location)`? But besides, it would be better if the function raised an exception instead of returning an empty string if it fails to return a meaningful value. – mkrieger1 Jun 06 '18 at 16:36
  • tried that. It throws an error. It does not know that as a type since first instance in my code of that object being created is when the function that populates myObj.location succeeds – TMWP Jun 06 '18 at 16:51
  • To your other comment - the function is populating a data table. In this case, it is desirable to not have it raise an exception but to explain that I would have to walk you through a rather lengthy piece of code. There is a reason why this simple test allows the code to work right. I was looking to see if there was a way to get around my workaround for better readability. – TMWP Jun 06 '18 at 16:53
  • `isinstance` works with user-defined classes. If you got an error, you have some other bug. We can't tell what that bug may be. – user2357112 Jun 06 '18 at 16:59
  • I tried feeding in `py.location,Location` and `"py.location.Location"` to `isinstance` but it did not take it. I see now that there are ways to feed in non-base classes but they have to be built already for the system to recognize them (as per the first answer just posted). question maybe should be reworded a bit on that point. This is not a user defined class per se but rather one that comes from the library in use and may get created after the `isinstance` test I need to perform. – TMWP Jun 06 '18 at 17:04
  • `py.location,Location` has a comma instead of a period, and `py` instead of `geopy`. – user2357112 Jun 06 '18 at 17:28
  • I typed in the original question, but not in the code where I experienced what I was describing. I have since corrected this in the question. – TMWP Jun 07 '18 at 18:18

1 Answers1

2

isinstance() works for user defined classes.

>>> class Foo: pass
...
>>> foo = Foo()
>>> isinstance(foo, Foo)
True
>>> isinstance(foo, str)
False
>>>
Sharad
  • 9,282
  • 3
  • 19
  • 36