Private attribute declared but not usedPTC-W0037
A private attribute defined in the class should be only accessible inside the class.
The names of attributes which are supposed to be public should not start with _
or __
.
This issue concerns that one or more private attributes have been declared but are nowhere used in the class.
It is recommended either to remove the unused private attributes or remove the __
prefix if it is intended to be accessed from outside the class.
Note:
A private attribute can not be accessed like other attributes.
Consider a class MyClass
with a private attribute: __x
.
Now, doing this will raise an AttributeError
:
c = MyClass()
print(c.__x)
But you can access the private attribute 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 attribute is being used in the class it is declared.
- Rename the attribute to comply with the conventions (Check References).
- If the attribute is unnecessary, remove it.
class Example:
def __init__(self, attr=10):
self.__private = "Placeholder"
self.attr = attr
def init_val(self):
print(f"class was initialised with the value: {self.attr}")
Here, the attribute self.__private
is unused and would not be accessible outside of the class directly.
Note: This attribute would be removed during autofix.