type(foo) == function ?

T

Tom Plunket

I'd like to figure out if a given parameter is a function or not.

E.g.
True

implies:
.... pass
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'function' is not defined

Is there a way I can know if 'foo' is a function?

thanks,
-tom!
 
C

Chris Mellon

I'd like to figure out if a given parameter is a function or not.

E.g.

... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'function' is not defined

Is there a way I can know if 'foo' is a function?
.... pass
....

But you probably want the callable() builtin instead.
 
E

Erik Max Francis

Hi Tom.

Tom said:
I'd like to figure out if a given parameter is a function or not.

E.g.

... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'function' is not defined

Is there a way I can know if 'foo' is a function?

The type of a function is types.FunctionType:
True

However, this doesn't take into account methods (MethodType and
UnboundMethodType). In dynamically-typed languages in general, explicit
typechecks are not a good idea, since they often preclude user-defined
objects from being used. Instead, try performing the call and catch the
resulting TypeError:
.... f()
.... except TypeError:
.... print "oops, f is not callable"
....
oops, f is not callable
 
T

Tom Plunket

Erik said:
In dynamically-typed languages in general, explicit typechecks are not
a good idea, since they often preclude user-defined objects from being
used. Instead, try performing the call and catch the resulting
TypeError:

Good point, although I need to figure out if the thing can be called
without calling it, so I can build an appropriate UI. Basically I
expect three types of things in the 'value' field of the top-level
dictionary. The three sorts of things that I will deal with in the UI
are callable things (e.g. functions, for which Chris Mellon reminds me
about callable()), mappings (e.g. dictionaries, used similarly to the
top-level one), and sequences of strings.

So I think callable() works for me in the general case, but now
trawling the documentation in that area I'm not sure how I can tell if
something is a mapping or if it's a sequence.

The gist of the UI generation may be envisioned as:

key is the name that gets assigned to the control.
value indicates that the UI element is a:
"group box" if the value is a mapping
series of "radio buttons" if the value is a sequence of strings
"check box" if the value is a function

....I've still gotta figure out the exact API, this is for a plugin
sort of system that'll be used by the manually-driven version of the
build process and this data is explicitly to build the UI for the
various tools that are available.

thanks,
-tom!
 
C

Chris Mellon

Good point, although I need to figure out if the thing can be called
without calling it, so I can build an appropriate UI. Basically I
expect three types of things in the 'value' field of the top-level
dictionary. The three sorts of things that I will deal with in the UI
are callable things (e.g. functions, for which Chris Mellon reminds me
about callable()), mappings (e.g. dictionaries, used similarly to the
top-level one), and sequences of strings.

So I think callable() works for me in the general case, but now
trawling the documentation in that area I'm not sure how I can tell if
something is a mapping or if it's a sequence.

The gist of the UI generation may be envisioned as:

key is the name that gets assigned to the control.
value indicates that the UI element is a:
"group box" if the value is a mapping
series of "radio buttons" if the value is a sequence of strings
"check box" if the value is a function

...I've still gotta figure out the exact API, this is for a plugin
sort of system that'll be used by the manually-driven version of the
build process and this data is explicitly to build the UI for the
various tools that are available.

Since a sequence can be viewed as a special case of a mapping (a dict
with integer keys) I don't think there's a good general case way to
tell.

You could try indexing it with a string, a sequence will throw a
TypeError, while a mapping will give you a result or a KeyError.

You could also just rely on isinstance() checks.
 
F

Fredrik Lundh

Mathias said:
This builtin will be removed in python 3.0!

that's far from clear (since the replacement approach, "just call it",
simply doesn't work). and if you track the Py3K discussions, you'll
notice current threads about *more* support for interface testing, not
less support.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top