Métodos estáticos de la clase Python
Quiero crear un tipo de clase de utilidad que contenga solo métodos estáticos que puedan llamarse con el prefijo de clase de nombre. Parece que estoy haciendo algo mal :)
Aquí está mi pequeña clase:
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
Ahora mi método "principal":
from FileUtility import *
def main():
path = 'C:\config_file_list.txt'
dir = FileUtility.GetFilePath(path)
print dir
y me salió un error:unbound method GetFilePath() must be called with FileUtility instance as first argument (got str instance instead)
.
Tengo algunas preguntas aquí:
¿Qué estoy haciendo mal? ¿No debería ser invocable el método estático por nombre de clase?¿Realmente necesito una clase de utilidad, o hay otras formas de lograr lo mismo en Python?Si intento cambiar el código principal, obtengo:TypeError: GetFilePath() takes exactly 1 argument (2 given)
El nuevomain
:
from FileUtility import *
def main():
objFile = FileUtility()
path = 'H:\config_file_list.txt'
dir = objFile.GetFilePath(path)
print dir