ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 26 Mar 2013 02:15:32 +0100How to get a Boolean from the type of an object?https://ask.sagemath.org/question/9933/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)Fri, 22 Mar 2013 07:02:18 +0100https://ask.sagemath.org/question/9933/how-to-get-a-boolean-from-the-type-of-an-object/Answer by Bétréma for <p>I am trying to define the <code>__init__</code> 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 <code>is_a_matrix(A)</code> and <code>is_a_tuple_of_pairs(A)</code> (and of course more generally testing <code>A</code> to be of some other type)</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/9933/how-to-get-a-boolean-from-the-type-of-an-object/?answer=14672#post-id-14672For matrices, I don't know, this is a complex topic in Sage. For tuples of pairs, this is pure Python, and I suggest:
def is_a_pair(a):
return isinstance(a,tuple) and len(a)==2
def is_a_tuple_of_pairs(A):
if not isinstance(A,tuple):
return False
return all (is_a_pair(a) for a in A)
Note that if you accept lists in addition to tuples, you can use:
isinstance(a,(list,tuple))
inside these functions and so on.Fri, 22 Mar 2013 08:44:08 +0100https://ask.sagemath.org/question/9933/how-to-get-a-boolean-from-the-type-of-an-object/?answer=14672#post-id-14672Comment by Amri for <p>For matrices, I don't know, this is a complex topic in Sage. For tuples of pairs, this is pure Python, and I suggest:</p>
<pre><code>def is_a_pair(a):
return isinstance(a,tuple) and len(a)==2
def is_a_tuple_of_pairs(A):
if not isinstance(A,tuple):
return False
return all (is_a_pair(a) for a in A)
</code></pre>
<p>Note that if you accept lists in addition to tuples, you can use:</p>
<pre><code>isinstance(a,(list,tuple))
</code></pre>
<p>inside these functions and so on.</p>
https://ask.sagemath.org/question/9933/how-to-get-a-boolean-from-the-type-of-an-object/?comment=18012#post-id-18012Thanks Bétréma; it seems I can also use `isinstance(A, type(matrix()))` to check if something is a matrix (though I am not sure if this is the best way).Tue, 26 Mar 2013 02:15:32 +0100https://ask.sagemath.org/question/9933/how-to-get-a-boolean-from-the-type-of-an-object/?comment=18012#post-id-18012