By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,496 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

class instance scope

P: n/a
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

Jul 24 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ritesh Raj Sarraf wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 24 '06 #2

P: n/a

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

Jul 24 '06 #3

P: n/a
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

Jul 24 '06 #4

P: n/a
You could possibly make the log class a singleton or a borg.

Jeethu Rao

Ritesh Raj Sarraf wrote:
>>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

Jul 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.