getter/setter must have 1 or 2 arguments respectively

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

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)

Learn More

Non-Compliant Code Examples

class Foo:
   @property
   def get_my_attribute(self, foo):  # getter should have only one argument
      return self.my_attribute
   
   @attr.setter
   def set_attr(self, v, bar):  # setter should have only two arguments
      self._attr = v

   @attr.deleter
   def del_attr(self, foo):  # deleter should have only one argument
      del self._attr

Compliant Code Examples

class Foo:
   def get_my_attribute(self):
      return self.my_attribute
   
   def get_my_attribute(self, foo): # Not a property or attr, valid
      return self.my_attribute

   @property
   def get_my_attribute(self):
      return self.my_attribute

   def set_my_attribute(self, v):
      self.my_attribute = v

   @attr.setter
   def set_attr(self, v):
      self._attr = v

   @attr.deleter
   def del_attr(self,):
      return self._attr
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: rtrieu/product-analytics-ui-changes