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__': ,
              'get_near_dups': ,
              'add': ,
              'delete': ,
              'offsets': ,
              'get_keys': ,
              'bucket_size': ,
              '__dict__': ,
              '__weakref__': ,
              '__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 

4. type() 函数

type(object)返回对象object的类型。

>>> type(mser)
'cv2.MSER'>
>>> type(mser.detect)
'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


你可能感兴趣的:(推荐系统)