2014-01-06 23 views
2

Dive into Pythonpython info函數:它在哪裏?

Python有一個稱爲info的函數。現在嘗試一下,然後瀏覽 列表。

>>> from apihelper import info 
>>> import __builtin__ 
>>> info(__builtin__, 20) 
ArithmeticError  Base class for arithmetic errors. 
AssertionError  Assertion failed. 
AttributeError  Attribute not found. 
EOFError    Read beyond end of file. 
EnvironmentError  Base class for I/O related errors. 
Exception   Common base class for all exceptions. 
FloatingPointError Floating point operation failed. 
IOError    I/O operation failed. 

原來,Python的有一個名爲信息功能。這本書是指什麼?

回答

5

我不知道爲什麼Dive Into Python聲稱「Python有一個叫info的函數」,但它顯然不是真的。

apihelper.py(從中導入info)在本書前面的in section 4.1中已有描述,它只是Mark Pilgrim爲Dive Into Python編寫的一個模塊。 apihelper模塊顯然確實包含一個info函數,它可以完成您鏈接的第4.3.3節中聲明的內容。

0

Python中沒有這樣的模塊,即apihelper.py但diveintopython的在他們的教程

def info(object,spacing=10,collapse=1): 
     availableMethod = [method for method in dir(object) if callable(getattr(object,method))]; 
     processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s) 
     print "\n".join(["%s %s" % (method.ljust(spacing),processFunc(getattr(object, method).__doc__)) for method in availableMethod]) 

做出功能這是函數他們已經開發出,他們在文件apihelper.py中指的是它,這就是爲什麼

from apihelper import info 

希望它有幫助。 :)