PyChecker must execute script?

Trent Mick trentm at ActiveState.com
Wed Feb 5 18:09:05 EST 2003


[Just wrote]
> In article <mailman.1044481572.29181.python-list at python.org>,
>  Trent Mick <trentm at ActiveState.com> wrote:
> 
> > [Geoff Gerrietts wrote]
> > > I believe PyChecker imports the modules, and analyzes the resulting
> > > bytecode.
> > 
> > Yes.
> 
> What I don't understand is if it analyzes *byte code* then why it must 
> do a full import? Wouldn't a plain compile() suffice then? There must be 
> more to it.

It doesn't need to it just does for historical reasons. My understanding
is that the PyChecker folks are working on a version of PyChecker that
does not need to import the module. I am not involved though so don't
know the details.


> > To be more specific. If you put all top-level executing code within a
> >     if __name__ == "__main__":
> >         # top-level code here
> > block then you will be fine.
> 
> A class or def statement is also top-level executing code, as are any 
> constant definitions and imports, so this definition is way to strict...

Unless I have been operating under gross misconceptions only top-level
code and "top-level class code" is executed on module import. For
example:

    ------ mymodule.py --------------------------------
    
    # This code is executed on import
    foo = 1
    bar()

    def spam():
        # The "def" statement is executed on import but the contents of
        # the routine are NOT.

    class Eggs:
        # The following two lines are executed on import
        foo = 1
        bar()

        # Code inside method definitions are NOT executed om import
        def __init__(self):
            pass
        

    if __name__ == "__main__":
        # These lines are NOT executed on import
        foo = 1
        bar()
    ---------------------------------------------------

In my experience this is not strict at all. In fact, this is
demonstrated by the usefulness of running PyChecker on the entire Python
Standard Library.


Cheers,
Trent

-- 
Trent Mick
TrentM at ActiveState.com





More information about the Python-list mailing list