Reputation: 291
I have a class called Route (with its own __repr__() function), and an instance of a Route called default_route. However, if I call isinstance(default_route,Route), it unexpectedly returns False, viz:
[Dbg]>>> default_route
Route(office(235)=323-654-3242)
[Dbg]>>> isinstance(default_route,Route)
False
[Dbg]>>> default_route.__class__
<class 'route.Route'>
[Dbg]>>> Route
<class 'route.Route'>
[Dbg]>>> type(default_route)
<class 'route.Route'>
[Dbg]>>> type(default_route) is Route
False
The class definition is really straightforward and uncomplicated:
class Route(object):
def __init__(self, phone, condition=None):
self.phone=phone
self.condition=condition
self.choice_name=None
I'm just baffled by this; why would isinstance() not return True above?
Upvotes: 14
Views: 13215
Reputation: 291
I am pretty sure this was a problem with the debugger I was using (Pythonwin); no matter how many times I reloaded and re-ran my code in that instance of the debugger, the problem was completely steady-state, and, as since I was in the process of debugging my code, I was stumped as to what to do next. However, given some of the comments above about reloading, I started to suspect something might be wrong with the debugging environment itself, so I closed the program, reopened it, and suddenly the problem was no longer reproducible.
Sorry to have bothered everyone, but at least if someone sees the same problem, maybe this will make them realize they're not crazy, and that Python itself isn't broken in some subtle way!
Upvotes: 0
Reputation: 155353
This could easily happen if you did the following:
import route
and create default_route
using route.Route
reload(route)
to pull in some code changes to the module (or just for fun; doesn't matter if the module is unchanged, reload
will still reload it)You could encounter this in a similar way if default_route
was also defined in route
, and you did from route import Route
, then reload
-ed route
, then did from route import default_route
(order of importing Route
and default_route
is irrelevant, as long as a reload
occurred between them, and the older one was not reimported). Either way, you have an instance and a class from subtly different versions of the module; they might look the same, have the same behaviors, etc., but they are not the same module, and therefore not the same classes, instances, etc.
In either case, a quick check would be to invoke default_route.__class__ is Route
; if that evaluates to True
, it's possible you've got some weird ABC
based class with a broken __subclasshook__
, but more likely it will evaluate to False
, indicating that the two classes, despite the matching name, actually originate from independent loads of the module.
Upvotes: 17