I'm assuming a private static method in a Python class is something that can and should be done. But perhaps in actuality, I should simply be using a module private method outside the class.
I want to understand calling different kinds of static methods from various locations:
I have a Python class with a private and a public static method. I want to call them from other places, and from each other.
When outside the class and calling the public static method, I must add the classname. i.e.
m = MyClass.the_staticmethod(100) # I must use the classname as a prefix
See questions in code:
class Myclass():
@staticmethod
__my_privatestaticmethod(myparam):
return myparam
@staticmethod
def the_staticmethod(myparam):
# will the following work?
result = __my_staticmethod(1) # will this work?
# data-mingling set as private, so following line cannot work!
result = Myclass.__my_staticmethod(2) # this cannot work.
result = the_staticmethod(3) # will this work without the prefix
return result
def __my_privatemethod(self, param1):
# which of the following are valid?
return __my_staticmethod(11) # will this work?
# data-mingling set as private, so following line cannot work!
return Myclass.__my_staticmethod(12) # this cannot work.
return the_staticmethod(13) # will this work without the prefix of the class?
return self.the_staticmethod(14) # will this work. Is the self also considered the class?
return Myclass.the_staticmethod(15) # this of course works.
def the_method(param1):
return __my_staticmethod(param1) # will this work?
If the answers to 1 and 11 are no, then the conclusion is that you cannot make a private static method.
I would then make a private module method outside the class without a decorator. This would be equivalent to the private static class method.
def __my_privatemodulemethod(param1):
return param1
and can call it from anywhere in my module, without a prefix.

所有评论(0)