【问题标题】:Nicely print "public" methods of python class很好地打印python类的“公共”方法
【发布时间】:2019-10-07 20:57:55
【问题描述】:

我正在尝试使用一个类在 python 中设计一个 API:

class SimulationApi(object):

  def hello(self):
    return "Hi"

  def echo(self, string):
    return string

  def get_foo(self):
    return self.foo

  def __init__(self):
    self.foo = 50

我想打印一个由该类定义的可用公共方法列表来控制台。有没有办法自动实现这一点,它也会获取方法参数?理想情况下,输出应如下所示:

SimulationApi: 
    get_foo()
    echo(string)
    hello()

到目前为止,我的解决方案是这样的,但它并不完整,可能是错误的方向。

print("SimulationApi: \n\t{}\n".format("\n\t".join([x+"()" for x in dir(SimulationApi) if not x.startswith("__")]))

【问题讨论】:

  • 其实这个还不错!请注意,已经有 help 方法可能有用,而不是重新编码
  • 你不能只使用像 sphinx 这样的文档工具吗?
  • 这对 api 的下一次迭代很有好处,我想知道是否有一种简单的方法可以将参数放入第一阶段。如果 sphinx 中有用于 API 早期阶段的有用工具,那么它也可以工作。
  • 你想要 pydoc 模块 (docs.python.org/3.7/library/pydoc.html) - 它是支持内置 help 系统的模块,它知道如何从类/函数定义和文档字符串生成文本和 html 文档。
  • 也许您想用元类自动生成__doc__ 属性?

标签: python introspection pretty-print


【解决方案1】:

你可以使用inspect模块:

class SimulationApi(object):

  def hello(self):
    return "Hi"

  def echo(self, string):
    return string

  def get_foo(self):
    return self.foo

  def __init__(self):
    self.foo = 50


import inspect
inspect.getmembers(SimulationApi)

将返回:

[('__class__', type),
 ('__delattr__', <slot wrapper '__delattr__' of 'object' objects>),
 ('__dict__',
  mappingproxy({'__module__': '__main__',
                'hello': <function __main__.SimulationApi.hello(self)>,
                'echo': <function __main__.SimulationApi.echo(self, string)>,
                'get_foo': <function __main__.SimulationApi.get_foo(self)>,
                '__init__': <function __main__.SimulationApi.__init__(self)>,
                '__dict__': <attribute '__dict__' of 'SimulationApi' objects>,
                '__weakref__': <attribute '__weakref__' of 'SimulationApi' objects>,
                '__doc__': None})),
 ('__dir__', <method '__dir__' of 'object' objects>),
 ('__doc__', None),
 ('__eq__', <slot wrapper '__eq__' of 'object' objects>),
 ('__format__', <method '__format__' of 'object' objects>),
 ('__ge__', <slot wrapper '__ge__' of 'object' objects>),
 ('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>),
 ('__gt__', <slot wrapper '__gt__' of 'object' objects>),
 ('__hash__', <slot wrapper '__hash__' of 'object' objects>),
 ('__init__', <function __main__.SimulationApi.__init__(self)>),
 ('__init_subclass__', <function SimulationApi.__init_subclass__>),
 ('__le__', <slot wrapper '__le__' of 'object' objects>),
 ('__lt__', <slot wrapper '__lt__' of 'object' objects>),
 ('__module__', '__main__'),
 ('__ne__', <slot wrapper '__ne__' of 'object' objects>),
 ('__new__', <function object.__new__(*args, **kwargs)>),
 ('__reduce__', <method '__reduce__' of 'object' objects>),
 ('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>),
 ('__repr__', <slot wrapper '__repr__' of 'object' objects>),
 ('__setattr__', <slot wrapper '__setattr__' of 'object' objects>),
 ('__sizeof__', <method '__sizeof__' of 'object' objects>),
 ('__str__', <slot wrapper '__str__' of 'object' objects>),
 ('__subclasshook__', <function SimulationApi.__subclasshook__>),
 ('__weakref__', <attribute '__weakref__' of 'SimulationApi' objects>),
 ('echo', <function __main__.SimulationApi.echo(self, string)>),
 ('get_foo', <function __main__.SimulationApi.get_foo(self)>),
 ('hello', <function __main__.SimulationApi.hello(self)>)]

注意:您的方法(您希望获取信息的方法)也是SimulationApi 类字典__dict__

您可以像这样获取echo 函数的完整代码:

import inspect
lines = inspect.getsource(SimulationApi.echo)
print(lines)

  def echo(self, string):
    return string

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    相关资源
    最近更新 更多