用于将函数作为属性进行比较的 Python 代码检查器

发布于 2024-10-27 13:56:26 字数 376 浏览 4 评论 0原文

我偶尔会花费大量时间来追踪代码中的脑残……虽然我通常会针对它运行 pylint,但有些东西会忽略 pylint。对我来说最容易忽视的问题是……

# normally, variable is populated from parsed text, so it's not predictable
variable = 'fOoBaR'
if variable.lower == 'foobar':
    #       ^^^^^<------------------ should be .lower()
    do_something()

pylint 和 Python 都没有对此提出异议……是否有一个 python 代码检查工具可以标记这个特定问题?

I occasionally spend a considerable amount of time tracking down brainfarts in my code... while I normally run pylint against it, there are some things that slip past pylint. The easiest problem for me to overlook is this...

# normally, variable is populated from parsed text, so it's not predictable
variable = 'fOoBaR'
if variable.lower == 'foobar':
    #       ^^^^^<------------------ should be .lower()
    do_something()

Neither pylint nor Python bark about this... is there a python code-checking tool that can flag this particular issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丿*梦醉红颜 2024-11-03 13:56:26

您建议如何使用代码检查器来验证这一点?这是完全合法的语法。与其检查此类错误,不如养成使用更好模式的习惯。

而不是:这样

variable = 'fOoBaR'
if variable.lower == 'foobar':
    #       ^^^^^<------------------ should be .lower()
    do_something()

做:

variable = 'fOoBaR'
sane_variable = variable.lower()
if sane_variable == 'foobar':
    do_something()

这样,您总是显式地对要比较的值调用 .lower(),而不是依赖就地方法调用和比较,这会导致到你正在经历的陷阱。

How do you propose a code-checker validate this? It's perfectly legitimate syntax. Rather than checking for this kind of mistake, it would be better to get into the habit of using better patterns.

Instead of:

variable = 'fOoBaR'
if variable.lower == 'foobar':
    #       ^^^^^<------------------ should be .lower()
    do_something()

Do this:

variable = 'fOoBaR'
sane_variable = variable.lower()
if sane_variable == 'foobar':
    do_something()

This way you're always explicitly calling .lower() on the value you're comparing against, instead of relying on an in-place method-call and comparison, which leads to the very pitfall you're experiencing.

寄居者 2024-11-03 13:56:26

@Mike Pennington 我只想首先说我也经常遇到这种情况 -.-

@eyquem 'lower()' 是一个函数。 “lower”是一个函数指针(如果我没记错的话)。 Python 将允许您尝试运行此代码,但不会调用该函数。

我认为这很难捕捉的原因是你并不总是知道你调用方法的变量的类型。例如,假设我有 2 节课。

class Foo()
   def func(self):
      #do stuff
      pass

class Bar()
   self.func = "stuff"

如果您的代码中有一个函数接受参数“baz”,如下所示:

def myfunction(baz):
    print baz.func

def myfunction(baz):
    baz.func()

根据 baz 的类型,其中任何一个都可能有效。
但实际上无法知道 baz 的类型是“Foo”还是“Bar”。

编辑:我的意思是静态分析......

@Mike Pennington I just want to first say that I also run into this a lot -.-

@eyquem 'lower()' is a function. 'lower' is a function pointer (if I'm not mistaken). Python will let you attempt to run this code, but it will not invoke the function.

I think the reason this is hard to catch is that you don't always know the type of the variable which you're calling methods on. For example, say I have 2 classes.

class Foo()
   def func(self):
      #do stuff
      pass

class Bar()
   self.func = "stuff"

If your code has a function in it that takes an argument 'baz' like so:

def myfunction(baz):
    print baz.func

def myfunction(baz):
    baz.func()

Either one of these could be valid depending on baz's type.
There is literally no way of knowing if baz is of type 'Foo' or 'Bar', though.

EDIT: I meant with static analysis...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文