Without compilation, how to find bugs?

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Fri Oct 14 19:37:18 EDT 2016


On Friday, October 14, 2016 at 2:05:01 AM UTC-7, BartC wrote:
> On 14/10/2016 01:59, sohcahtoa82 at gmail.com wrote:
> > On Thursday, October 13, 2016 at 4:06:36 PM UTC-7, pozz wrote:
> 
> >> Are the things exactly how I understood, or do I miss something in Python?
> >
> > As others have said, user a linter.
> 
> With Python you're supposed to just be able run any source code 
> instantly; how will using a 'lint' tool impact that process? Or is it 
> only meant to be used infrequently?

I'm not the person to ask about lint usage in Python.  I don't use a linter.  I just use the static analysis built into my IDE.

> 
> > I'd go a step further and use an actual code editor or IDE that includes some basic static analysis.  Using this example that Skip used:
> >
> > def func2(a, b):
> >     print(a, b)
> >
> > def func1(a):
> >     print(a)
> >
> > func2(1)
> >
> > Any code editor worth using will highlight the ) on the last line and tell you that there's a missing parameter.
> 
> How can that work? I thought one of the biggest deals with Python is 
> that you can re-bind function names to anything else. So:
> 
> if cond:
>      func2 = 38
> else:
>      func2 = func1
> 
> Then func2(1) can either be perfectly correct, or completely erroneous!
> 
> (I have my own suspicions that functions in Python are predominantly 
> used in a boring, predictable, static manner (so allowing certain 
> optimisations - or error checking), but I got the impression from some 
> threads here that many apparently do little else in their code but bind 
> and rebind function names.)
> 
> -- 
> Bartc

If you don't know if a variable is an integer or a function, then you've got other problems.  That just doesn't look like clean code.

And yes, I know, Python doesn't have "variables", but rather objects bound to names, or names bound to objects, or however you want to say it, the idea is similar.

Back on track, in the case you mentioned, my IDE does not flag any errors.  Even if I do this:

def func(y):
    return y

x = 0
if x:
    f = 1
else:
    f = func

f(0)

My IDE highlights the f(0) line and says that f is not callable.  Obviously it's wrong, but that just shows that my IDE's static analysis has its limits.

(For the record, my IDE is PyCharm 2.7.3, which I know is HORRENDOUSLY out of data, but it's what I'm stick with at work for now)

Anyways...if at any point in the execution of a program there's uncertainty whether a name could be bound to an integer OR a function, that seems like a code smell, especially when it's name looks like a function.



More information about the Python-list mailing list