From 48acc9f6bcffc47188701cdf58b2c88d8f762035 Mon Sep 17 00:00:00 2001 From: xujian Date: Wed, 6 Dec 2023 02:22:41 +0000 Subject: [PATCH 1/2] =?UTF-8?q?update=20=E7=BC=96=E7=A0=81=E8=A7=84?= =?UTF-8?q?=E8=8C=83/openKylin-python=E8=AF=AD=E8=A8=80=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E7=BC=96=E7=A8=8B=E8=A7=84=E8=8C=83.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xujian --- ...26\347\250\213\350\247\204\350\214\203.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git "a/\347\274\226\347\240\201\350\247\204\350\214\203/openKylin-python\350\257\255\350\250\200\351\200\232\347\224\250\347\274\226\347\250\213\350\247\204\350\214\203.md" "b/\347\274\226\347\240\201\350\247\204\350\214\203/openKylin-python\350\257\255\350\250\200\351\200\232\347\224\250\347\274\226\347\250\213\350\247\204\350\214\203.md" index 3d8e28b..f164eb1 100644 --- "a/\347\274\226\347\240\201\350\247\204\350\214\203/openKylin-python\350\257\255\350\250\200\351\200\232\347\224\250\347\274\226\347\250\213\350\247\204\350\214\203.md" +++ "b/\347\274\226\347\240\201\350\247\204\350\214\203/openKylin-python\350\257\255\350\250\200\351\200\232\347\224\250\347\274\226\347\250\213\350\247\204\350\214\203.md" @@ -579,6 +579,62 @@ new_exception.__cause__ = chained_exception def static_method(): print("static_method") +## 规则3.5 属性的getter、setter和deleter方法应该有预期数量的参数(python:S5724) + +### 错误代码 + class A: + @property + def foo(self, unexpected, unexpected2): # Noncompliant. Too many parameters. + return self._foo + + @foo.setter + def foo(self, value, unexpected): # Noncompliant. Too many parameters. + self._foo = value + + @foo.deleter + def foo(self, unexpected): # Noncompliant. Too many parameters. + del self._foo + + class B: + def get_foo(self, unexpected): # Noncompliant. Too many parameters. + return self._foo + + def set_foo(self, value, unexpected): # Noncompliant. Too many parameters. + self._foo = value + + def del_foo(self, unexpected): # Noncompliant. Too many parameters. + del self._foo + + foo = property(get_foo, set_foo, del_foo, "'foo' property.") + +### 正确代码 + class A: + @property + def foo(self): + return self._foo + + @foo.setter + def foo(self, value): + self._foo = value + + @foo.deleter + def foo(self): + del self._foo + + class B: + def get_foo(self): + return self._foo + + def set_foo(self, value): + self._foo = value + + def del_foo(self): + del self._foo + + foo = property(get_foo, set_foo, del_foo, "'foo' property.") +### 参考资料 +[python内置函数-属性](https://docs.python.org/zh-cn/3/library/functions.html#property) + # 4 控制流 ## 规则4.1 所有代码都应该是可达的(python:S1763) -- Gitee From b9724eea322762e0c9baaa9310ed480cd2d76c70 Mon Sep 17 00:00:00 2001 From: xujian Date: Wed, 6 Dec 2023 02:31:04 +0000 Subject: [PATCH 2/2] =?UTF-8?q?update=20=E7=BC=96=E7=A0=81=E8=A7=84?= =?UTF-8?q?=E8=8C=83/openKylin-python=E8=AF=AD=E8=A8=80=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E7=BC=96=E7=A8=8B=E8=A7=84=E8=8C=83.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xujian --- ...47\274\226\347\250\213\350\247\204\350\214\203.md" | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git "a/\347\274\226\347\240\201\350\247\204\350\214\203/openKylin-python\350\257\255\350\250\200\351\200\232\347\224\250\347\274\226\347\250\213\350\247\204\350\214\203.md" "b/\347\274\226\347\240\201\350\247\204\350\214\203/openKylin-python\350\257\255\350\250\200\351\200\232\347\224\250\347\274\226\347\250\213\350\247\204\350\214\203.md" index f164eb1..5d0e43a 100644 --- "a/\347\274\226\347\240\201\350\247\204\350\214\203/openKylin-python\350\257\255\350\250\200\351\200\232\347\224\250\347\274\226\347\250\213\350\247\204\350\214\203.md" +++ "b/\347\274\226\347\240\201\350\247\204\350\214\203/openKylin-python\350\257\255\350\250\200\351\200\232\347\224\250\347\274\226\347\250\213\350\247\204\350\214\203.md" @@ -580,6 +580,17 @@ new_exception.__cause__ = chained_exception print("static_method") ## 规则3.5 属性的getter、setter和deleter方法应该有预期数量的参数(python:S5724) +属性的getter、setter和deleter方法由python解释器以特定的数字或者参数进行调用: + +1、属性的getter和deleter方法只有self参数 + +2、属性的setter方法有两个参数:self参数和value参数 + +添加其他参数或者删除这些强制参数,都会导致方法调用失败。例如以下情况: + +1、getter、setter和deleter方法中定义了超过预期数量的参数 + +2、setter方法中缺少value参数 ### 错误代码 class A: -- Gitee