Python class static methods Python class static methods python python

Python class static methods


You're getting the error because you're taking a self argument in each of those functions. They're static, you don't need it.

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

#fileutility.py:def get_file_size(fullName):    fileSize = os.path.getsize(fullName)    return fileSizedef 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 the PYTHONPATH)

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

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


You really shouldn't be creating static methods in Python. What you should be doing is putting them at the global function level, and then accessing the module they're in when you call them.

foo.py:

def bar():  return 42

baz.py:

import fooprint foo.bar()


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.