By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

type(foo) == function ?

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

E.g.
>>type(1)
<type 'int'>
>>type(1) == int
True

implies:
>>def foo():
.... pass
....
>>type(foo)
<type 'function'>
>>type(foo) == function
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!
Nov 29 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 11/29/06, Tom Plunket <to***@fancy.orgwrote:
I'd like to figure out if a given parameter is a function or not.

E.g.
>type(1)
<type 'int'>
>type(1) == int
True

implies:
>def foo():
... pass
...
>type(foo)
<type 'function'>
>type(foo) == function
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?
>>def foo():
.... pass
....
>>from inspect import isfunction
isfunction(foo)
True
>>>

But you probably want the callable() builtin instead.
thanks,
-tom!
--
http://mail.python.org/mailman/listinfo/python-list
Nov 29 '06 #2

P: n/a
Hi Tom.

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

E.g.
>>>type(1)
<type 'int'>
>>>type(1) == int
True

implies:
>>>def foo():
... pass
...
>>>type(foo)
<type 'function'>
>>>type(foo) == function
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:
>>import types
types.FunctionType
<type 'function'>
>>def f(): pass
....
>>type(f)
<type 'function'>
>>type(f) 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 = 'asdf'
try:
.... f()
.... except TypeError:
.... print "oops, f is not callable"
....
oops, f is not callable

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
There are not fifty ways of fighting, there is only one: to be the
conqueror. -- Andrew Malraux, 1937
Nov 29 '06 #3

P: n/a
Tom Plunket wrote:
I'd like to figure out if a given parameter is a function or not.
http://effbot.org/pyref/callable

</F>

Nov 29 '06 #4

P: n/a
Erik Max Francis wrote:
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!
Nov 29 '06 #5

P: n/a
On 11/29/06, Tom Plunket <to***@fancy.orgwrote:
Erik Max Francis wrote:
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.
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.
thanks,
-tom!
--
http://mail.python.org/mailman/listinfo/python-list
Nov 29 '06 #6

P: n/a
Chris Mellon wrote:
On 11/29/06, Tom Plunket <to***@fancy.orgwrote:
>I'd like to figure out if a given parameter is a function or not.

E.g.
>>type(1)
<type 'int'>
>>type(1) == int
True

implies:
>>def foo():
... pass
...
>>type(foo)
<type 'function'>
>>type(foo) == function
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?
>>>def foo():
... pass
...
>>>from inspect import isfunction
isfunction(foo)
True
>>>>


But you probably want the callable() builtin instead.
This builtin will be removed in python 3.0!
>thanks,
-tom!
--
http://mail.python.org/mailman/listinfo/python-list
Dec 1 '06 #7

P: n/a
Mathias Panzenboeck wrote:
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>

Dec 2 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.