class instance scope

R

Ritesh Raj Sarraf

Hi,

I have a class defined in a file called foo.py

In bar.py I've imported foo.py
In bar.py's main function, I instantiate the class as follows:

log = foo.log(x, y, z)

Now in main I'm able to use log.view(), log.error() et cetera.

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

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 ?

Ritesh
 
S

Steve Holden

Ritesh said:
Hi,

I have a class defined in a file called foo.py

In bar.py I've imported foo.py
In bar.py's main function, I instantiate the class as follows:

log = foo.log(x, y, z)

Now in main I'm able to use log.view(), log.error() et cetera.
Correct. Because, having instantiated the class and retained a reference
to the instance, the methods of the instance are available relative to
the name containing the reference.
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.
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.

regards
Steve
 
R

Ritesh Raj Sarraf

Steve said:
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.
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
 
R

Ritesh Raj Sarraf

log = foo.log(x, y, z)
Resulting line is:
log = foo.log(x, y, z)
global log

Making the instance "log" global makes it accessible to all the
functions.

Now I have only one question, Is this a correct way to do it ? Or are
there better way ?

Ritesh
 
D

Dennis Lee Bieber

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.
It will be global to the module -- ie, the FILE -- in which that
instantiation took place.

If you have something like:

import this
import that

log = this.log(...)
that.func()

where "that.func" has a line saying "log.something()", "log" is NOT
global to "that"
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top