Reputation: 103
What is wrong with this code and is that implementation correct?
from enum import Enum
class Test(object):
Filters = Enum('Filters', 'A B C')
def __init__(self):
pass
def aaa(self, filters):
if(isinstance(filters, self.Filters.B)):
print 'OK'
else:
print 'NOT OK'
if __name__ == '__main__':
Test().aaa(Test.Filters.B)
Error is:
Traceback (most recent call last):
File "test.py", line 14, in <module>
Test().aaa(Test.Filters.B)
File "test.py", line 9, in aaa
if(isinstance(filters, Test.Filters.B)):
TypeError: isinstance() arg 2 must be a type or tuple of types
Upvotes: 2
Views: 3421
Reputation: 69021
If you want to know if the filters
parameter is a member of the Test.Filters
Enum, you have three choices
isinstance(filters, Test.Filters)
filters in self.Filters
(Test
and self
are interchangeable.)
If you want to know if the filters
parameter is Test.Filters.B
then a simple comparison will work:
filters is self.Filters.B
Upvotes: 3
Reputation: 2224
self.Filters.B
is not defined. You defined
Filters.B
Also:
self.Filters.B
needs to be:
type(self.Filters.B)
it works like this:
class A():
pass
isinstance(3, int) == True
isinstance(3,type(4)) == True
isinstance("hello", type('something')) == True
isinstance("hello", str) == true
isinstance(A(), type(A())) == True
isinstance(A(), A) == True
Upvotes: -1
Reputation: 82889
The error you get is quite clear:
TypeError: isinstance() arg 2 must be a type or tuple of types
Filters.B
is not a class or type. Instead of using isinstance
, just compare the value that you got with the one you want:
if filters is Test.Filters.B:
Also, Filters
is an attribute of the class, not of the instance, so you should probably rather use Test.Filters
, although self.Filters
seems to work, too.
Upvotes: 2