Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To check if <code>o</code> is an instance of <code>str</code> or any subclass of <code>str</code>, use <a href="https://docs.python.org/3/library/functions.html#isinstance" rel="noreferrer">isinstance</a> (this would be the "canonical" way):</p> <pre><code>if isinstance(o, str): </code></pre> <p>To check if the type of <code>o</code> is exactly <code>str</code>:</p> <pre><code>if type(o) is str: </code></pre> <p>The following also works, and can be useful in some cases:</p> <pre><code>if issubclass(type(o), str): </code></pre> <p>See <a href="http://docs.python.org/2/library/functions.html" rel="noreferrer">Built-in Functions</a> in the Python Library Reference for relevant information.</p> <p>One more note: in this case, if you're using python 2, you may actually want to use:</p> <pre><code>if isinstance(o, basestring): </code></pre> <p>because this will also catch Unicode strings (<a href="https://docs.python.org/2/library/functions.html#unicode" rel="noreferrer"><code>unicode</code></a> is not a subclass of <code>str</code>; both <code>str</code> and <code>unicode</code> are subclasses of <a href="https://docs.python.org/2/library/functions.html#basestring" rel="noreferrer"><code>basestring</code></a>). Note that <code>basestring</code> no longer exists in python 3, where there's <a href="https://docs.python.org/3.0/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit" rel="noreferrer">a strict separation</a> of strings (<a href="https://docs.python.org/3/library/functions.html#func-str" rel="noreferrer"><code>str</code></a>) and binary data (<a href="https://docs.python.org/3/library/functions.html#func-bytes" rel="noreferrer"><code>bytes</code></a>).</p> <p>Alternatively, <code>isinstance</code> accepts a tuple of classes. This will return True if x is an instance of any subclass of any of (str, unicode):</p> <pre><code>if isinstance(o, (str, unicode)): </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload