gpt4 book ai didi

python - 很好地打印了 python 类的 "public"方法

转载 作者:行者123 更新时间:2023-12-01 00:56:12 25 4
gpt4 key购买 nike

我正在尝试使用类在 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

我想打印到控制台由该类定义的可用公共(public)方法的列表。有没有一种方法可以自动执行此操作,同时也会获取方法参数?理想情况下,输出如下所示:

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("__")]))

最佳答案

您可以使用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

关于python - 很好地打印了 python 类的 "public"方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56238719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com