I want to create a kind of utility class which contains only static methods which are callable by the name class prefix. Looks like I'm doing something wrong :)
Here is my small class:
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
Now my "main" method:
from FileUtility import *
def main():
path = 'C:\config_file_list.txt'
dir = FileUtility.GetFilePath(path)
print dir
and I got an error: unbound method GetFilePath() must be called with FileUtility instance as first argument (got str instance instead)
.
A have a few questions here:
- 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)
The new main
:
from FileUtility import *
def main():
objFile = FileUtility()
path = 'H:\config_file_list.txt'
dir = objFile.GetFilePath(path)
print dir
Static methods don't get the object passed in as the first parameter (no object)
remove the
self
parameter and the calls should work. The import problem is relevant too. And the static comment relevant too.