From: Peng Yu on
Hi,

'open' is not a function according to inspect module. But according to
help(open), it is a function. Is there something wrong with inspect
module?

$ cat main.py
#!/usr/bin/env python

import inspect

def hello():
print "Hello World!"
return

print inspect.isfunction(str)
print inspect.isfunction(open)
print inspect.isfunction(hello)

$ ./main.py
False
False
True


>>>help(open)
Help on built-in function open in module __builtin__:

open(...)
open(name[, mode[, buffering]]) -> file object

Open a file using the file() type, returns a file object. This is the
preferred way to open a file.


--
Regards,
Peng
From: Ian Kelly on
On Tue, Jun 22, 2010 at 9:42 AM, Peng Yu <pengyu.ut(a)gmail.com> wrote:
> Hi,
>
> 'open' is not a function according to inspect module. But according to
> help(open), it is a function. Is there something wrong with inspect
> module?
>
> $ cat main.py
> #!/usr/bin/env python
>
> import inspect
>
> def hello():
>  print "Hello World!"
>  return
>
> print inspect.isfunction(str)
> print inspect.isfunction(open)
> print inspect.isfunction(hello)

inspect.isfunction returns True if the object is a *user-defined*
function. For built-in functions, use inspect.isbuiltin. You might
also be interested in the callable() function, which returns True if
the object is any object that can be called.

Cheers,
Ian
From: James Mills on
On Wed, Jun 23, 2010 at 1:42 AM, Peng Yu <pengyu.ut(a)gmail.com> wrote:
> 'open' is not a function according to inspect module. But according to
> help(open), it is a function. Is there something wrong with inspect
> module?

$ python
Python 2.6.5 (r265:79063, Jun 13 2010, 14:03:16)
[GCC 4.4.4 (CRUX)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from inspect import isbuiltin, isclass, isfunction
>>> type(str)
<type 'type'>
>>> isbuiltin(str), isclass(str), isfunction(str)
(False, True, False)
>>> def foo(): pass
....
>>> type(foo)
<type 'function'>
>>> isbuiltin(foo), isclass(foo), isfunction(foo)
(False, False, True)
>>> type(open)
<type 'builtin_function_or_method'>
>>> isbuiltin(open), isclass(open), isfunction(open)
(True, False, False)
>>>

Notice anything ?

--James

PS: The Python REPL is your friend :)

--
--
-- "Problems are solved by method"
From: Terry Reedy on
On 6/22/2010 11:42 AM, Peng Yu wrote:

> 'open' is not a function according to inspect module.

If you want to *learn* Python, perhaps you should ignore the inspect
module. It is for advanced purposes. I only used it a couple of times in
13 years.

If you want to know what something literally is, you can usually just
print it.

In 3.1
>>> list
<class 'list'>
>>> list.append
<method 'append' of 'list' objects>
>>> [].append
<built-in method append of list object at 0x00EFDCB0>
>>> open
<built-in function open>
>>> def f():
def __init__(self): pass


>>> f
<function f at 0x00F00C90>
>>> f.__init__
<method-wrapper '__init__' of function object at 0x00F00C90>

These are all 'callables' , meaning that you can call them by appending
'(args)'. For many purposes, the different classes of callables are not
important. Some of the details above were different in 2.x.

Sometimes you need to call type(ob) instead, if its string
representation is not informative.

>>> help
Type help() for interactive help, or help(object) for help about object.
>>> type(help)
<class 'site._Helper'>

Help is a builtin instance of site._Helper, which has a custom .__str__
method that prints the instruction instead of the usual sort of
description. Since we can call 'help', we may presume site._Helper has a
..__call__ method that makes its instances callable. Let us check:

>>> dir(type(help))
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__']

dir() is another tool that I use all the time.

Terry Jan Reedy


From: Mel on
Peng Yu wrote:

> Hi,
>
> 'open' is not a function according to inspect module. But according to
> help(open), it is a function. Is there something wrong with inspect
> module?
>
> $ cat main.py
> #!/usr/bin/env python
>
> import inspect
>
> def hello():
> print "Hello World!"
> return
>
> print inspect.isfunction(str)
> print inspect.isfunction(open)
> print inspect.isfunction(hello)

help (inspect.isfunction) gives

Help on function isfunction in module inspect:

isfunction(object)
Return true if the object is a user-defined function.


Mel.