Python Introspection
sys modules
- 查詢目前所在的作業系統 (OS)
>>> import sys
>>> sys.platform
'darwin'
- Max Int
>>> sys.maxint
2147483647
- 目前已經讀取的 modules (sys.modules)
查詢 Object
- 名字 (name)
>>> dir.__name__
'dir'
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.)
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.).
沒有留言:
張貼留言