Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isinstance returning false when class is the same?

Tags:

python

def addError(self, e):
    if not isinstance(e, Error):
        raise ValueError('{0} is not type {0}'.format(e, Error))
    self.__errors.append(e)

Message:

ValueError: <class 'api.utils.Error'> is not type <class 'api.utils.Error'>

like image 213
slik Avatar asked Jan 24 '14 03:01

slik


People also ask

What data type is returned from Isinstance function?

Return Type of isinstance() in Python This function returns a boolean value, i.e., True or False. It will return True if the object class matches the classinfo class; otherwise, False.

Does Isinstance check subclass?

isinstance() checks whether or not the object is an instance or subclass of the classinfo.

What are the differences between type () and Isinstance ()?

What Are Differences Between type() and isinstance()? and isinstance() is that type(object) returns the type of an object and isinstance(object, class ) returns True if the object argument is an instance of the class argument or in a direct or indirect subclass relationship.

What is the purpose of Isinstance () function?

The isinstance() function returns True if the specified object is of the specified type, otherwise False . If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.


1 Answers

You're passing the class itself, not an instance of the class. That explains your problem.

>>> class A:
    pass

>>> isinstance(A, A)
False

What you probably want is to check an instance:

>>> isinstance(A(), A)
True
like image 168
aIKid Avatar answered Oct 05 '22 23:10

aIKid