在Python中使用与脚本名相同的类名有什么区别?

2024-04-18 18:11:57 发布

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

我希望在python脚本中使用与python脚本名称相同的类名。而且我的脚本中只有一个类。例如,如果我的脚本结构如下所述:

test.py
  |
  class test(object):
    def __init__(self, var1, var2 ....):
      ...
      ...  

我想知道当我导入这个脚本并从其他脚本使用这个类时会有什么不同?在


Tags: pytestself脚本名称objectinitdef
1条回答
网友
1楼 · 发布于 2024-04-18 18:11:57

纯粹从语言的角度来看,除了可能与导入类的人产生一些混淆之外,它不会有任何区别。在

如果将此模块用作robotframework测试套件中的库,那么robot将自动实例化与库文件同名的类。该类中的方法将作为该库中的关键字公开。在

robotframework用户指南有一个标题为Creating test library class or module的部分,上面写着(emphasis mine):

Python classes are always inside a module. If the name of a class implementing a library is the same as the name of the module, Robot Framework allows dropping the class name when importing the library. For example, class MyLib in MyLib.py file can be used as a library with just name MyLib. This also works with submodules so that if, for example, parent.MyLib module has class MyLib, importing it using just parent.MyLib works. If the module name and class name are different, libraries must be taken into use using both module and class names, such as mymodule.MyLibrary or parent.submodule.MyLib.

相关问题 更多 >