Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:python-best-practices/get-set-arguments
Language: Python
Severity: Warning
Category: Best Practices
Description
Ensure that getter and setter have the right number of parameters:
getters must have exactly one parameter (the instance we are reading from)
setters must have exactly two parameters (the instance we are updating and the associated value)
classFoo:@propertydefget_my_attribute(self,foo):# getter should have only one argumentreturnself.my_attribute@attr.setterdefset_attr(self,v,bar):# setter should have only two argumentsself._attr=v@attr.deleterdefdel_attr(self,foo):# deleter should have only one argumentdelself._attr
Compliant Code Examples
classFoo:defget_my_attribute(self):returnself.my_attributedefget_my_attribute(self,foo):# Not a property or attr, validreturnself.my_attribute@propertydefget_my_attribute(self):returnself.my_attributedefset_my_attribute(self,v):self.my_attribute=v@attr.setterdefset_attr(self,v):self._attr=v@attr.deleterdefdel_attr(self,):returnself._attr
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- python-best-practices # Rules to enforce Python best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines