473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

type(foo) == function ?

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
7 1471
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: seash | last post by:
H i need a function like GetExitCodeProcess() type function in c#, cause i need to know the status of a process how do i track the status of a process , whether the process is running or exited ...
3
by: Tuvas | last post by:
I am building a GUI interface with Tkinter. I need to have a way to open and save files. Is there a nice GUI that can do that for me, ei, show what files are avaliable, a choose file type function?...
10
by: mypetrock | last post by:
Has anyone run into this error message? Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'. I'm trying to cast an object of type Foo.Bar that I got out of a hash table into a variable...
20
by: elderic | last post by:
Hi there, are there other ways than the ones below to check for <type 'function'> in a python script? (partly inspired by wrapping Tkinter :P) def f(): print "This is f(). Godspeed!" 1.:...
6
by: Arne Schmitz | last post by:
Why do I get the following error: error: type 'std::vector<TObject*, std::allocator<TObject*' is not derived from type 'bar<TObject>' with the following code: #include <vector> ...
7
by: oscartheduck | last post by:
Hi folks, I'm trying to alter a program I posted about a few days ago. It creates thumbnail images from master images. Nice and simple. To make sure I can match all variations in spelling of...
5
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.