python shell方法列表? [英] list of methods for python shell?

查看:134
本文介绍了python shell方法列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用术语,您已经发现我是python n00b.

You'd have already found out by my usage of terminology that I'm a python n00b.

直截了当的问题:

如何在交互式python外壳中看到特定对象的方法列表,就像我在ruby中可以看到的那样(您可以在ruby irb中用对象后的'.methods来做到这一点)?

How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?

推荐答案

现有答案可以很好地向您展示如何获取对象的属性,但不能准确回答您提出的问题-如何获取对象的方法. Python对象具有统一的名称空间(与Ruby不同,在Ruby中,方法和属性使用不同的名称空间).考虑例如:

Existing answers do a good job of showing you how to get the ATTRIBUTES of an object, but do not precisely answer the question you posed -- how to get the METHODS of an object. Python objects have a unified namespace (differently from Ruby, where methods and attributes use different namespaces). Consider for example:

>>> class X(object):
...   @classmethod
...   def clame(cls): pass
...   @staticmethod
...   def stame(): pass
...   def meth(self): pass
...   def __init__(self):
...     self.lam = lambda: None
...     self.val = 23
... 
>>> x = X()
>>> dir(x)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
 '__getattribute__', '__hash__', '__init__', '__module__',
 '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
 '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
 'clame', 'lam', 'meth', 'stame', 'val']

((为便于阅读,请进行输出拆分).

((output split for readability)).

如您所见,这为您提供了所有属性的名称-包括许多仅继承自object的特殊方法,特殊数据属性,例如__class____dict____doc__ -instance数据属性(val),按实例的可执行属性(lam)以及实际方法.

As you see, this is giving you the names of all attributes -- including plenty of special methods that are just inherited from object, special data attributes such as __class__, __dict__ and __doc__, per-instance data attributes (val), per-instance executable attributes (lam), as well as actual methods.

如果需要进一步选择时,请尝试:

If and when you need to be more selective, try:

>>> import inspect
>>> [n for n, v in inspect.getmembers(x, inspect.ismethod)]
['__init__', 'clame', 'meth']

标准库模块inspect是在Python中进行自省的最佳方法:它基于内置自省钩子(例如dir和更高级的钩子)构建,为您提供有用,丰富而简单的信息自省服务.例如,在这里,您看到仅显示了由此类专门设计的实例和类方法-没有显示静态方法,没有显示实例属性(无论是否可调用),没有显示从object继承的特殊方法.如果您的选择性需求略有不同,则可以轻松构建自己的调整后的ismethod版本并将其作为getmembers的第二个参数传递,以根据您的精确需求量身定制结果.

Standard library module inspect is the best way to do introspection in Python: it builds on top of the built-in introspection hooks (such as dir and more advanced ones) to offer you useful, rich, and simple introspection services. Here, for example, you see that only instance and class methods specifically designed by this class are shown -- not static methods, not instance attributes whether callable or not, not special methods inherited from object. If your selectivity needs are slightly different, it's easy to build your own tweaked version of ismethod and pass it as the second argument of getmembers, to tailor the results to your precise, exact needs.

这篇关于python shell方法列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆