Property defined with parametersPYL-R0206
A class property
should not define any additional parameters since it can not be passed while accessing it.
If a property with additional parameters is attempted to be accessed using additional parameters, those parameters would be applied to the return value of the called property, which can result in an unintended bug (explained in the example below).
If passing parameters is absolutely necessary, it is recommended to convert the property to a method.
Bad practice
class SomeClass:
@property
def get_my_lucky_number(self, num=5):
if num > 5:
return num
return 0
Here, the property get_my_lucky_number
has been defined with a default argument num
.
A call like SomeClass().get_my_lucky_number(num=7)
will raise TypeError: 'int' object is not callable
.
This is because parameter is passed to the return value of the property, which is an int
here.
Recommended
A class property shouldn't have any parameters. Here's how a property signature should look like:
class SomeClass:
@property
def get_my_lucky_number(self):
return 5