python中物件屬性
阿新 • • 發佈:2019-02-13
1. dir() 函式
dir([object]) 會返回object所有有效的屬性列表。示例如下:
dir(SimhashIndex) Out[111]: ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add', 'bucket_size', 'delete', 'get_keys', 'get_near_dups', 'offsets']
2. vars() 函式
vars([object]) 返回object物件的__dict__屬性,其中object物件可以是模組,類,例項,或任何其他有__dict__屬性的物件。所以,其與直接訪問__dict__屬性等價。示例如下(當然也有一些物件中沒有__dict__屬性):
vars(SimhashIndex) Out[112]: mappingproxy({'__module__': 'simhash', '__init__': <function simhash.SimhashIndex.__init__(self, objs, f=64, k=2, log=None)>, 'get_near_dups': <function simhash.SimhashIndex.get_near_dups(self, simhash)>, 'add': <function simhash.SimhashIndex.add(self, obj_id, simhash)>, 'delete': <function simhash.SimhashIndex.delete(self, obj_id, simhash)>, 'offsets': <property at 0x8e7ad68>, 'get_keys': <function simhash.SimhashIndex.get_keys(self, simhash)>, 'bucket_size': <function simhash.SimhashIndex.bucket_size(self)>, '__dict__': <attribute '__dict__' of 'SimhashIndex' objects>, '__weakref__': <attribute '__weakref__' of 'SimhashIndex' objects>, '__doc__': None})
3. help() 函式
help([object])呼叫內建幫助系統。輸入
按h鍵,顯示幫助資訊; 按 q 鍵,退出。
help(SimhashIndex) Help on class SimhashIndex in module simhash: class SimhashIndex(builtins.object) | Methods defined here: | | __init__(self, objs, f=64, k=2, log=None) | `objs` is a list of (obj_id, simhash) | obj_id is a string, simhash is an instance of Simhash | `f` is the same with the one for Simhash | `k` is the tolerance | | add(self, obj_id, simhash) | `obj_id` is a string | `simhash` is an instance of Simhash | | bucket_size(self) | | delete(self, obj_id, simhash) | `obj_id` is a string | `simhash` is an instance of Simhash | | get_keys(self, simhash) | | get_near_dups(self, simhash) | `simhash` is an instance of Simhash | return a list of obj_id, which is in type of str | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | offsets | You may optimize this method according to <http://www.wwwconference.org/www2007/papers/paper215.pdf>
4. type() 函式
type(object)返回物件object的型別。
>>> type(mser)
<type 'cv2.MSER'>
>>> type(mser.detect)
<type 'builtin_function_or_method'>
5. hasattr() 函式
hasattr(object, name)用來判斷name(字串型別)是否是object物件的屬性,若是返回True,否則,返回False
>>> hasattr(mser, 'detect')
True
>>> hasattr(mser, 'compute')
False
6. callable() 函式
callable(object):若object物件是可呼叫的,則返回True,否則返回False。注意,即使返回True也可能呼叫失敗,但返回False呼叫一定失敗。
>>> callable(mser.detect)
True