Python class static methods

I want to create a utility class that contains only static methods that are called by the class prefix of the name. 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 the error message: unbound method GetFilePath() must be called with FileUtility instance as first argument (got str instance instead) .

A ask a few questions here:

  • What am I doing wrong? Should a static method be called by a class?
  • Do I need a utility class, or are there any other ways to achieve this in Python?
  • If I try to change the code basically, I get: TypeError: GetFilePath() takes exactly 1 argument (2 given)

New main :

 from FileUtility import * def main(): objFile = FileUtility() path = 'H:\config_file_list.txt' dir = objFile.GetFilePath(path) print dir 
+46
python static
Oct 04 '12 at 20:30
source share
7 answers

You get an error because you accept the self argument in each of these functions. They are static, you do not need it.

However, the "pythonic" way to do this is not to have a class full of static methods, but simply make them free functions in the module.

 #fileutility.py: def get_file_size(fullName): fileSize = os.path.getsize(fullName) return fileSize def get_file_path(fullName): filePath = os.path.abspath(fullName) return filePath 

Now in your other python files (assuming fileutility.py is in the same directory or on PYTHONPATH )

 import fileutility fileutility.get_file_size("myfile.txt") fileutility.get_file_path("that.txt") 

It doesn't mention static methods specifically, but if you come from a different PEP 8 language, the python style guide is a good read and introduction as python programmers think.

+79
04 Oct '12 at 20:35
source share

You really should not create static methods in Python. What you have to do is place them at the global level of functions, and then access the module they are in when you call them.

foo.py:

 def bar(): return 42 

baz.py:

 import foo print foo.bar() 
+8
04 Oct.
source share

Static methods do not pass the object passed as the first parameter (no object)

remove the self parameter and the calls should work. The import problem is also relevant. And a static comment too.

+6
Oct 04 '12 at 20:35
source share

In python, java-like methods (or something else) static are not widely used, since they really have no purpose.

Instead, you should simply define your "methods" as functions in the module:

 #module1.py def fun1(): return do_stuff() def fun2(arg): return do_stuff_with_arg(arg) #main.py import module1 if __name__ == '__main__': a = module1.fun() print module1.fun2(a) 
+4
04 Oct
source share

Just remove self in the method definition. Your intention is to use as static. I have to work with an instance of this class.

+2
Sep 07 '15 at 11:37
source share

If you want to use your functions defined in a class, you just need to instantiate your class and apply the function.

So the result is:

 dir = FileUtility().GetFilePath(path) 

Just add () after your class name.

@staticmethod is not required as you are using a standard function, not a static one. But in your case, the result will be the same.

+1
Jun 16 '14 at 8:55
source share

Just remove self in the function definition. Since you use static functions, you do not need to pass yourself as an argument to functions. So your class and function should be like this:

 class FileUtility(): @staticmethod def GetFileSize(fullName): fileSize = os.path.getsize(fullName) return fileSize @staticmethod def GetFilePath(fullName): filePath = os.path.abspath(fullName) return filePath 
0
Jan 06 '17 at 18:56
source share



All Articles