Why 'open' is not a function according to inspect module?

Terry Reedy tjreedy at udel.edu
Tue Jun 22 14:49:48 EDT 2010


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





More information about the Python-list mailing list