Ask Your Question

Revision history [back]

How to get a Boolean from the type of an object?

I am trying to define the __init__ of a class, where I would like to support different types of input; in my case the input could be a either a list of pairs or a matrix. How do I write a Boolean function to test which kind of input I have? In the example code, I want to know how I should define the Boolean functions is_a_matrix(A) and is_a_tuple_of_pairs(A) (and of course more generally testing A to be of some other type)

class someclass():
    def __init__(self, A):
        if is_a_matrix(A):
            self._matrix = A
        if is_a_tuple_of_pairs(A):
            self._matrix = somefunction(A)