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

Modules, Packages and Developer Confusion. Oh My!

P: n/a
I think I'm still missing something in how python is handling packages
and it's mixing me up. I have a package with three files (modules?)
like so:

OPS:\
__init__.py
model.py
search.py

To hide more details of the package structure, I import model and
search inside of __init__. It also seemed like a good idea to define a
global function that creates a database connection and I added it to
__init__.py. Thus, I have:
from model import *
from search import *

def create_connection():
# details are unimportant for this example
When I try to use the create_connection function in model, I get errors
when I use it as a global function ( just create_connection()). The
only way to resolve the error was to import OPS inside of model and use
OPS.create_connection(). This doesn't seem natural. If model is part of
OPS, why do I need to tell python to import OPS and use this function
from OPS? I can see doing that from the outside world, but inside?

Any clarification would be greatly appreciated.

Nov 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
dh****@gmail.com wrote:
from model import *
from search import *

def create_connection():
# details are unimportant for this example
When I try to use the create_connection function in model, I get errors
when I use it as a global function ( just create_connection()).
importing everything from "model" into "OPS" isn't the same thing as
importing everything from "OPS" into "model".

</F>

Nov 10 '06 #2

P: n/a
dh****@gmail.com wrote:
I think I'm still missing something in how python is handling packages
and it's mixing me up. I have a package with three files (modules?)
like so:

OPS:\
__init__.py
model.py
search.py

To hide more details of the package structure, I import model and
search inside of __init__. It also seemed like a good idea to define a
global function that creates a database connection and I added it to
__init__.py. Thus, I have:
from model import *
from search import *

def create_connection():
# details are unimportant for this example
When I try to use the create_connection function in model, I get errors
when I use it as a global function ( just create_connection()). The
only way to resolve the error was to import OPS inside of model and use
OPS.create_connection(). This doesn't seem natural. If model is part of
OPS, why do I need to tell python to import OPS and use this function
from OPS? I can see doing that from the outside world, but inside?

Any clarification would be greatly appreciated.
The def statement binds the name create_connection in the model module's
namespace to the function it defines. So you write EITHER

import model # puts model in the current namespace
conn = model.create_connection( ... )

OR

from model import * # copies model's namespace into current
conn = create_connection( ... )

Hope this helps.

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

Nov 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.