python help

class base(object):
 def help1(self,object1, spacing,collapse=1):
  """Print method and doc string.
  Takes module, class, list, dictionary, or string."""
  typeList = (BuiltinFunctionType, BuiltinMethodType, FunctionType, MethodType, ClassType)
  methodList = [method for method in dir(object1) if type(getattr(object1, method)) in typeList]
  processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s:s)
  #print ["%s" % method.ljust(1) for method in methodList]
  #print type(spacing),spacing
  print "\n".join(["%s %s" % (method.ljust((spacing)), processFunc(str(getattr(object1, method).__doc__))) for method in methodList])
 def __init__(self):
  ab=[]
  self.help1(ab,10,1)

 在红体字上,如果第一个参数不是self,而是object1直接作为第一个参数,python默认的就会把object1作为self参数的功能来用(虽然名称不一样),打个比方,如果此处函数help1,定义为def help1(object1, spacing,collapse=1): 此时object1会被当作self,而spacing就会被传递值ab 而collapse的值就会等于10,而此时python的解释器就会看到你调用时传递的参数多于help1定义时的参数而报错。此处需要注意。

该类用来实现help的API,用户可以调用此类来实现查询该类型的help信息

 

posted @ 2013-05-26 16:26  SA高处不胜寒  阅读(286)  评论(0编辑  收藏  举报