Python中的静态方法不起作用

Python中的静态方法不起作用,python,python-2.7,Python,Python 2.7,我正在自学Python,我有一个令人震惊的问题,因为我不明白为什么它不工作。我正在使用PyDev,我已经下载了Python的第2版。我有以下代码: class Utils: @staticmethod def hello(): print "Hi! I'm the Utils class" Utils.hello() #Hi! I'm the Utils class 现在一切都很顺利。但是如果我导入Utils类并从另一个模块调用静态方法 import Uti

我正在自学Python,我有一个令人震惊的问题,因为我不明白为什么它不工作。我正在使用PyDev,我已经下载了Python的第2版。我有以下代码:

class Utils:

    @staticmethod
    def hello():
        print "Hi! I'm the Utils class"

Utils.hello() #Hi! I'm the Utils class
现在一切都很顺利。但是如果我导入Utils类并从另一个模块调用静态方法

import Utils

Utils.hello()
我得到这个错误:

Traceback (most recent call last):
  File "C:\Users\migugonz\Desktop\Docs\Per Folder\WorkSpacePy\Rosalind\src\bioinformatics\stronghold\Pruebas.py", line 40, in <module>
    Utils.hello()
AttributeError: 'module' object has no attribute 'hello'
回溯(最近一次呼叫最后一次):
文件“C:\Users\migugonz\Desktop\Docs\Per Folder\WorkSpacePy\Rosalind\src\bioinformatics\bundle\Pruebas.py”,第40行,在
Utils.hello()
AttributeError:“module”对象没有属性“hello”

我觉得这没什么大不了的,但我一直在寻找解决方案,只要我知道这是可行的。

我相信你需要做
Utils.Utils.hello()


或者像导入Utils一样导入Utils导入Utils

Python不是php:您不需要类来模拟名称空间。只要在Utils中将
hello
作为一个全局函数就行了。Python不是Java,这里绝对没有理由有一个类,特别是如果它只包含静态方法的话。只需将
hello
放在文件的顶层即可。谢谢大家。没错,我来自Java,我曾经有一个用于可重用方法的静态类。也许像你说的那样做更好,thg435。谢谢这就是为什么PEP8建议模块应具有所有小写名称。如果@hannibal使用了
utils.py
作为模块的名称,那么就不会有混淆模块和类的余地。目前,我的模块将是小写的。我很困惑,因为在java中(使用eclipse)文件的名称是类本身。