473,394 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

class instance scope

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
4 1639
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
2
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
2
by: Michael | last post by:
Hello, I have a newbie question about class scope. I am writting a little program that will move files to one of two empty folders. I am having a hard time understanding scope. So this will be a...
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.