Статические методы класса Python
Я хочу создать своего рода служебный класс, который содержит только статические методы, которые можно вызывать с помощью префикса класса имени. Похоже, я что-то делаю не так :)
Вот мой маленький класс:
class FileUtility():
@staticmethod
def GetFileSize(self, fullName):
fileSize = os.path.getsize(fullName)
return fileSize
@staticmethod
def GetFilePath(self, fullName):
filePath = os.path.abspath(fullName)
return filePath
Теперь моя & quot; главная & quot; метод:
from FileUtility import *
def main():
path = 'C:\config_file_list.txt'
dir = FileUtility.GetFilePath(path)
print dir
и я получил ошибку:unbound method GetFilePath() must be called with FileUtility instance as first argument (got str instance instead)
.
У меня есть несколько вопросов здесь:
What am I doing wrong? Should not the static method be callable by classname? Do I really need a utility class, or are there other ways to achieve the same in Python? If I try to change the code in main I'm getting:TypeError: GetFilePath() takes exactly 1 argument (2 given)
Новыйmain
:
from FileUtility import *
def main():
objFile = FileUtility()
path = 'H:\config_file_list.txt'
dir = objFile.GetFilePath(path)
print dir