我知道我可以使用“find Usages”来找出在类中调用方法的是什么。对于给定类上的所有方法,有没有这样做的方法?(或者文件中的所有方法)
用例:我正在尝试重构一个上帝类,几乎可以肯定的是,它应该是几个类。如果能够看到god类方法的哪个子集,与之交互的类使用它,那将是一件很好的事情。看起来PyCharm已经做到了这一点,但是不让我扩展它。
我使用的是PyCharm 2016.1.2
发布于 2016-05-12 01:35:28
这是可能的,但您必须处理抽象,否则Pycharm不知道有问题的方法属于您的特定类。又名- 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()
发布于 2018-08-30 13:56:56
如今,Pycharm使用Python3.6类型提示和“正确”匹配函数调用将变得相当容易,因为类型提示是Python3.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)不能正确解决这类调用,但我希望这种情况在未来会有所改变。
https://stackoverflow.com/questions/37031483
复制相似问题