用更新的方法更新Python脚本?

2024-06-26 01:41:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我借用了一个python插件用于我正在使用的应用程序。这个插件有点过时了,因为脚本中使用的方法已经更改,我想尝试找出如何编辑脚本,并对方法和函数进行适当的更新。脚本中使用了4个模块,我不知道哪个模块包含方法及其所有函数

基本上我有这样一句话:

layerEPSG = layer.srs().epsg()
projectEPSG = self.canvas.mapRenderer().destinationSrs().epsg()

srs()方法已更改为crs(),一些函数名也已更改(但仍执行相同的操作)。我想把它们列出来,看看是否有epsg()destinationSrs()的新名称

这在我看来是有道理的,但我对模块、类、方法和函数如何协同工作还没有完全的了解。这是一个学习更多的项目。你知道吗

感谢您的帮助, 迈克


Tags: 模块方法函数self脚本插件layer应用程序
2条回答

您可以使用dir()来发现模块的结构

import layers
# print out the items in the module layers
print dir(layers)
print

x = layer.crs()
# print out the type that crs() returns
print type(x)
# print out the methods on the type returned by crs()
print dir(x)

或者您可以打开模块并读取其代码。你知道吗

您还可以使用help()来提供有关类或模块的更多信息。例如:

>>> class Fantasy():
...     def womble(self):
...         print('I am a womble!')
...     def dragon(self):
...         """ Make the Dragon roar! """
...         print('I am a dragon...ROAR!')
...
>>> help(Fantasy)
Help on class Fantasy in module __main__:

class Fantasy(builtins.object)
 |  Methods defined here:
 |
 |  dragon(self)
 |      Make the Dragon roar!
 |
 |  womble(self)
 |
 |                                     
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)

当然,如果类/模块中有doc字符串,这会更有用。你知道吗

相关问题 更多 >