In Python, private methods are not enforced by the language, but are rather a convention that is followed by Python programmers. However, when it comes to method access, the concept of "private" still exists, and we might want to call a private method on an instance of a class.
To call a private static method on an instance of a class in Python, we can use the classmethod()
decorator to turn the private method into a class method, and then call it on the instance. Here is an example:
main.py313 chars12 lines
In this example, we define a private static method called __private_static_method()
using the staticmethod()
decorator. We then define an instance method called call_private_static_method()
which calls the private static method using the self
keyword, as if it were an instance method.
This works because Python allows us to call instance methods on objects of the class, and because a class method can be called from an instance method, as long as it is defined entirely within the class.
gistlibby LogSnag