Checking Python code to compare function as attribute

I sometimes spend a considerable amount of time tracking brain structures in my code ... while I usually run pylint against it, there are some things that slip past pylint. The simplest problem for me is ...

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

Neither pilint nor Python is about it ... is there a python code verification tool that can flag this problem?

+5
source share
3 answers
0
source

@ Mike Pennington. I just want to say first that I also often came across this. -

@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".

EDIT: ...

0

? . , ​​, .

:

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

:

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

That way, you always explicitly call .lower()on the value you are comparing, rather than relying on the invocation method and the comparison in place, which leads to the error itself that you are experiencing.

0
source

All Articles