python中对象属性

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(objectname)用来判断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


猜你喜欢

转载自blog.csdn.net/erinapple/article/details/80556138