473,396 Members | 1,689 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,396 software developers and data experts.

Namespace issue

Hi,

I need a little help in understanding how Namespaces and scoping works with
Classes/Functions in Python.

Here's my code:
class FetchData:
def __init__(self, dataTypes=["foo", "bar", "spam"], archive=False):

self.List = []
self.Types = dataTypes

if archive:
self.Archiver = Archiver(True)

def FetchData(self, PackageName, Filename=None):

try:
import my_module
except ImportError:
return False

if Filename != None:
try:
file_handle = open(Filename, 'a')
except IOError:
sys.exit(1)

(amnt, header, self.List) = my_module.get_data(PackageName)
This is the only way this code will work.

As per my understanding, the bad part is that on every call of the method
FetchData(), an import would be done.

To not let that happen, I can put the import into __init__(). But when I put
in there, I get a NameError saying that my_module is not available even
though it got imported.
All I noticed is that the import has to be part of the method else I end up
getting a NameError. But always importing my_module is also not good.

What is the correct way of doing this ?
IMO, ideally it should be part of __init__() and be imported only when the
class is instantiated.

Thanks,
Ritesh
--
If possible, Please CC me when replying. I'm not subscribed to the list.

May 23 '07 #1
0 751

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
2
by: Brian | last post by:
I am having a problem with namespace resolution. Example is below: Namespace MyNamespace Public Class MyClass public x as System.String end class end namespace I get the following compile...
7
by: Kevin Newman | last post by:
I've been toying with a namespace manager, and wanted to get some input. So what do you think? if (typeof com == 'undefined') var com = {}; if (!com.unFocus) com.unFocus = {}; ...
6
by: Robert Warnestam | last post by:
I've two class libraries, where one is referencing the other. The first library looks like this; (assembly name and root name space is set to Codab.Parser) namespace Codab.Parser { public class...
24
by: Tiraman | last post by:
Hi, I Build my own dll with my own namespace name and i would like to put it in one place but for the project bin folder so all of the projects will be able to use it . i tried to put the dll...
0
by: Charles Leonard | last post by:
I need opinions regarding Web Service Namespace usage and/or suggestions regarding how to handle deployment. Our web service application will exist in two different domains and is likely to...
2
by: Pathogenix | last post by:
Greetings, I'm trying to fix a web service client which has been implemented in a dailywtf worthy manner. I've decided to rip all the old code out and start again from the proxy generated by...
4
by: Kevin Newman | last post by:
The primary problem I've had with php is the lack of namespaces, which makes OOP very difficult to organize, since you end up with large number of classes cluttering up the same namespace - which...
2
by: TotySantana | last post by:
Now i am trying to change the namespace for the web page class by putting the page class between namespace myCustomNameSpace {} , but when i do this with a web page class i got the following error ...
6
by: bogus1one | last post by:
Hi All Given the following: // NamespaceTemplate.cpp : Defines the entry point for the console application. // #include "stdafx.h"
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.