LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-06-2007, 02:26 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
python module scope


Hello,

I have the following package:

Code:
./packagename/
  __init__.py:
    import string
    from module1 import *
    from module2 import *
  module1.py:
    function1(args):
      ...
      string.split(args)
      ...
  module2.py:
    function2():
      ...
      function1(args)
      ...
when i import packagename into python and try to use function2:
function2 calls on function1, but I am receiving an error that global name funcion1 is not defined. When i issue a dir(), both function1 and function2 are listed. Similarly, function1 calls on string.split, and it complains that global name 'string' is not defined. Whatever am I to do?

Thank you very much.

Last edited by jhwilliams; 08-06-2007 at 02:30 PM.
 
Old 08-08-2007, 09:25 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
String functions are instance methods: you call them on the string you want to split, for example -- "hello world".split(...) -- and then the result gets returned.

If you want to call module1.function1 from module2, you need to import module1 inside module2.

Unless I misunderstand what you want to do?
 
Old 08-08-2007, 09:29 AM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 208Reputation: 208Reputation: 208
Quote:
If you want to call module1.function1 from module2, you need to import module1 inside module2.
Yea - I ended up doing this to resolve the problem. I guess what was confusing to me is that I thought that the __init__ script would handle all of the dependencies. I guess I don't really understand what the __init__ script is meant for, afterall.

Also - good call on the .split() function.
 
Old 08-08-2007, 04:16 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
Quote:
Originally Posted by jhwilliams
I guess I don't really understand what the __init__ script is meant for, afterall.
Just off the top of my head (not near my Python reference), I've only used it before for using Java-like package management in Python 2.5 -- like the following:
Code:
from lexicon.pos import *
Where below the current directory there is a "lexicon" directory, and beneath that a "pos" directory, and inside that the modules I'm loading. For this to work, you need to have the __init__.py files in the "lexicon" and "pos" directories. My files were empty, but I know you can put code into there. I'm just not quite clear on when it gets loaded or what it's scope is... which I guess is what your question was. But I do know that it doesn't do what you originally intended, unfortunately.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scope of an object and global scope lucky6969b Programming 7 12-09-2005 10:09 PM
using distutils to install a python module shanenin Programming 2 11-29-2005 10:44 PM
Python inheritance and scope dakensta Programming 2 07-31-2005 07:16 AM
no python module base.g? hcgernhardt Linux - Software 0 01-15-2005 01:16 PM
Help me (python and MySQLdb module) Dark Carnival Programming 2 04-22-2004 08:31 AM


All times are GMT -5. The time now is 11:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration