版權聲明

所有的部落格文章都可以在右邊[blog文章原始檔案]下載最原始的文字檔案,並依你高興使用 docutil 工具轉換成任何對應的格式方便離線閱覽,除了集結成書販賣歡迎任意取用,引用

Python Introspection

Python Introspection

sys modules

  • 查詢目前所在的作業系統 (OS)
>>> import sys
>>> sys.platform
'darwin'
  • Max Int
>>> sys.maxint
2147483647

  • 目前已經讀取的 modules (sys.modules)

查詢 Object

  • 名字 (name)
>>> dir.__name__
'dir'

Type

查詢物件的 type

>>> type('e')
<type 'str'>

比較 type

>>> type('a') is types.StringType
True

Attributes

hasattr(object, name) getattr(object, name [,default])

>>> hasattr(id,'__doc__')
True
>>> print getattr(id,'__doc__')
id(object) -> integer

Return the identity of an object.  This is guaranteed to be unique among
simultaneously existing objects.  (Hint: it's the object's memory address.)

Callable

判斷是否能呼叫

>>> callable(dir)
True

Class 相關

>>> print isinstance.__doc__
isinstance(object, class-or-type-or-tuple) -> bool

Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
>>> print isinstance.__doc__
isinstance(object, class-or-type-or-tuple) -> bool

Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).

沒有留言:

Related Posts with Thumbnails