PyCharm:列出类的所有方法的所有用法

时间:2016-05-04 15:01:06

标签: python python-3.x python-2.7 intellij-idea pycharm

我知道我可以使用'Find Usages'来查找在课堂上调用方法的内容。 有没有办法为给定类的所有方法执行此操作? (或者确实是文件中的所有方法)

用例:我正在尝试重构一个神级,几乎可以肯定是几个类。能够看到上帝类方法的哪个子集,与它交互的类使用会很好。似乎PyCharm已经做到了这一点,但不允许我扩展它。

我正在使用PyCharm 2016.1.2

https://intellij-support.jetbrains.com/hc/en-us/community/posts/206666319-See-all-callers-of-all-methods-of-a-class

2 个答案:

答案 0 :(得分:-1)

这是可能的,但你必须处理抽象,否则Pycharm不知道有问题的方法属于你的特定类。 AKA - Type Hinting

在找不到类型提示的抽象层中调用该方法的任何实例都将无法找到。

示例:

#The class which has the method you're searching for.
class Inst(object):
    def mymethod(self):
        return

#not the class your looking for, but it too has a method of the same name.
class SomethingElse(object):
    def mymethod(self):
        return

#Option 1 -- Assert hinting
def foo(inst):
    assert isinstance(inst, Inst)
    inst.mymethod()

#Option 2 --  docstring hinting
def bar(inst):
    """

    :param inst: 
    :type inst: Inst 
    :return: 
    :rtype: 
    """
    inst.mymethod()

答案 1 :(得分:-1)

如今,Pycharm使用Python 3.6类型提示和“正确地”匹配函数调用将变得相当容易,因为类型提示是Python 3.5 / 3.6语言的一部分。当然,大型软件中的部分类型提示在解决方法调用的目标时会造成一些折衷。

这里是一个例子,类型提示如何使类型推断逻辑变得非常容易,并解决正确的调用目标。

def an_example():
    a: SoftagramAnalysisAction = SoftagramAnalysisAction(
        analysis_context=analysis_context,
        preprocessors=list(preprocessors),
        analyzers=list(analyzers),
        analysis_control_params=analysis_control_params)
    output = a.run()

在上面的示例中,局部变量a被专门标记为类型SoftagramAnalysisAction,这清楚地表明,run()调用在该类(或其任何可能的子类)的run方法的目标下方。

当前版本(2018.1)无法正确解决此类呼叫,但我希望将来会有所改变。