Private method declared but not usedPTC-W0038
A private method defined in the class should be only accessible inside the class.
The names of methods which are supposed to be public should not start with _
or __
.
This issue concerns that one or more private methods have been declared but are nowhere used in the class.
It is recommended either to remove the unused private methods or remove the __
prefix if it is intended to be accessed from outside the class.
Note:
A private method can not be accessed like other methods.
Consider a class MyClass
with a private method: __x
.
Now, doing this will raise an AttributeError
:
c = MyClass()
print(c.__x)
But you can access the private method like this (which is not recommended, just for the record):
c = MyClass()
print(c._MyClass__x)
So, if you want to fix this issue:
- Make sure the private method is being used in the class it is declared.
- Rename the method to comply with the conventions (Check References).
- If the method is unnecessary, remove it.
class Example:
def example_method(self):
print("I am accessible from outside the class")
def __private(self):
print("I am an unused Private method")
Here, the method __private
is unused and would not be accessible outside of the class directly.
Note: This method would be removed during autofix.