如何修复PyDev“方法应将自身作为第一个参数”错误 [英] How do I fix PyDev "Method should have self as first parameter" errors

查看:83
本文介绍了如何修复PyDev“方法应将自身作为第一个参数”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse中使用PyDev在Python中进行开发,而我的一些代码在代码分析工具中会产生错误。具体来说:

I'm developing in Python using PyDev in Eclipse, and some of my code generates errors in the code analysis tool. Specifically:

class Group(object):
    def key(self, k):
        class Subkey(object):
            def __enter__(s):
                self._settings.beginGroup(k)
                return self

            def __exit__(s, type, value, tb):
                self._settings.endGroup()

         return Subkey()

给我一​​个方法'__enter __- group'应该具有self作为第一个参数 错误,而 __ exit __ 。有没有一种方法可以解决此问题,而无需将 self 分配给另一个变量并在其他方法签名中重新使用该变量?

Gives me a "Method '__enter__- group' should have self as first parameter" error, and a similar error for __exit__. Is there a way to solve this without assigning self to another variable and reusing the variable in the other method signatures?

推荐答案

您可以在首选项中禁用该错误...

You could disable that error in the preferences...

Window > Preferences > Pydev > Editor > Code Analysis > Others

或重构代码...

class Group(object):
    def key(self, k):
        outer_self = self
        class Subkey(object):
            def __enter__(self):
                outer_self._settings.beginGroup(k)
                return outer_self

            def __exit__(self, type, value, tb):
                outer_self._settings.endGroup()

         return Subkey()

您还做什么期望?错误检查可以帮助您。如果您认为它们不是合法错误,请禁用它们或重构代码。

What else do you expect? The error checks are there to help you. If you don't think they're legitimate errors, disable them or refactor the code.

在这种情况下,我会说重构代码。就像King Radical的答案所证明的那样,它更具可读性。他不明白 s 是另一个自我

In this case I'd say refactor the code. It's more readable, as evidenced by King Radical's answer. He didn't understand that s was another self.

这篇关于如何修复PyDev“方法应将自身作为第一个参数”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆