class instance scope

Ritesh Raj Sarraf riteshsarraf at gmail.com
Mon Jul 24 07:41:38 EDT 2006


Steve Holden wrote:
> Ritesh Raj Sarraf wrote:
> > But when I call the same method from some functions which are in
> > bar.py, it fails giving me the following error:
> >
> > NameError: global name 'log' is not defined
> >
> Well, that's preumbaly because your
>
>    log = foo.log(x, y, z)
>
> statement was inside a function, and so the name "foo" was created in
> that function's local namespace rather than in the module's global
> namespace.
>

So if I do the instantiation before calling main(), will it work.
Something like:

if __name__ == "__main__":
    log = foo.log(x, y, z)
    main()

In this case, will log be global ?
But still I get the same error.

> > 1) I tried lookng into the docs but couldn't find anything on instance
> > scope.
> > 2) How is such situation tackled ? Will I have to instantiate in every
> > function ?
> >
> The best thing to do would be to pass the instance in as an argument to
> the functions that need to manipulate it.
>

But then how do os, sys, and other modules which are imported, become
accessible to all the functions ?

I'm a newbie, so please bear with me.

Ritesh




More information about the Python-list mailing list