Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to assert that it's actually a function, that's easy:</p> <pre><code>assert ininstance(dist_fun, types.FunctionType) </code></pre> <p>… or:</p> <pre><code>assert inspect.isfunction(dist_fun) </code></pre> <p>In general, when you have this problem:</p> <blockquote> <p>I've seen that the "type" of a function is "function", but Python does not allow me to use this as a type?</p> </blockquote> <p>… for a built-in type, the type you want is available in <a href="http://docs.python.org/2/library/types.html" rel="nofollow"><code>types</code></a> and/or a function that checks more simply can be found in <a href="http://docs.python.org/3/library/types.html" rel="nofollow"><code>inspect</code></a>.</p> <hr> <p>However, you probably don't want to do that; rather, you want to assert that it's <em>something callable</em>. After all, a bound method, a <code>functools.partial</code>, an object with a custom <code>__call__</code> method, a custom function-like object implemented in a C extension, etc. would all be just as useful here, right? So use <a href="http://docs.python.org/2/library/functions.html#callable" rel="nofollow"><code>callable</code></a>:</p> <pre><code>assert callable(dist_fun) </code></pre> <p>(Unless you need to support Python 3.0-3.1, or really ancient 1.x versions. <code>callable</code> was added somewhere around 1.4, removed in 3.0, then added back in 3.2.)</p> <hr> <p>Or, maybe even better (with Python 3.3+ only):</p> <pre><code>assert inspect.signature(dist_fun).bind(p1, p2) </code></pre> <p>This checks that it's not just callable, but callable with the arguments you want to pass it. (Not such a big deal when it's just two positional arguments… but when you're passing keyword arguments, or forwarding args with <em>/</em>*, etc., it can be.)</p>
    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. 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.
 

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