python - 您可以在不创建实例的情况下使用其他文件类中的函数吗?

标签 python python-2.7

文件一.py:

class classOne:
    def myFunc():
        print True

文件二.py:

from fileOne import classOne
classOne.myFunc()

还是我必须像这样制作 fileTwo.py:

from fileOne import classOne
instanceClass = classOne()
instanceClass.myFunc()

最佳答案

将“@staticmethod”放在方法定义之上:

>>> class classOne:
...     @staticmethod
...     def myFunc():
...             print True
... 
>>> classOne.myFunc()
True

关于python - 您可以在不创建实例的情况下使用其他文件类中的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24270223/

相关文章:

python - 获取protobuf当前时间戳

python - Django 如何查看生成的 SQL 查询?

python - Openpyxl:确定单元格值中的哪个字符是删除线

用于 beta 分发的 Python scipy 重载 _stats 函数

Python 元类与对象实例化一起使用

python - 使用自定义中间表的 Django ManyToMany 关系查询集

python - 在 Python 中对普通和 Unicode 空字符串进行 "not None"测试的最佳方法是什么?

python - macOS BigSur 上 python 版本的问题

python - 有没有一种 pythonic 的方法来获取字典中列表中的变量总数?

python - 如何按照我的查询(一个句子分解成一个词元组)的相同顺序对我的 SQL 结果进行排序?