Search the web
Sign In
New User? Sign Up
linux-nepal
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Python NameSpace Error   Message List  
Reply | Forward Message #612 of 658 |
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.




Wed May 23, 2007 6:20 pm

riteshsarraf
Online Now Online Now
Send Email Send Email

Forward
Message #612 of 658 |
Expand Messages Author Sort by Date

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...
Ritesh Raj Sarraf
riteshsarraf
Online Now Send Email
May 23, 2007
7:16 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help