473,396 Members | 1,767 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.

Confused about modules

Hi
I'm new to Python but old to programming. Using Python 2.5 on Windoze.

Trying to turn my program into a re-usable component and having all kinds of problems.

I have a class that represents a MySql database connection. In the constructor for the class it connects to the database, which creates an instance of a variable called DB.

Another class in the module needs DB access, so it gets passed the DB variable in the constructor.

Well this works with the code in the same file, but when this code gets moved into a module, the compiler seems to "forget" the method table for DB between calls. In other words when it runs it says "instance X has no attribute Y" -- put it all in the same file and it runs just fine.

WTF? When I open the database in the main file, and pass the connection to other modules it works just fine. Its as if the method table for an object instanced in a module other than main has some different kind of scoping rule.

Any ideas?
Sep 20 '07 #1
2 1128
bartonc
6,596 Expert 4TB
I have posted my implementations of your exact goal in the Articles Section.

The proper thing to do is pass the instance of your class that has the DB within it.
Given that instance variable, any module may then call any of the methods of that class without the need to import the module itself.

Have a look at the articles and then we can have a class/module primer here.

Thanks for joining.
Sep 20 '07 #2
Thanks got it all figured out. My confusion stemmed from Python's subtle syntax!!
Sep 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brian Roberts | last post by:
I'm confused about the use of hasattr/getattr, or possibly namespaces. I know how to do this: class UnderstandThis(object): def do_foo(self): pass def do_bar(self): pass def doit(self, cmd):...
0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
0
by: Rob R. Ainscough | last post by:
I'm having a problem with the VS.NET IDE (VB.NET) -- if I create a class/module in a project (in a solution) it is saved in my project root dir -- not a problem so far. However, if I decide that...
4
by: Misto . | last post by:
Hi folks! Short: There is a way to dumplicate a module ? I tried copy.deepcopy(module) but hangs with an error (also with standard modules ).. The only solution that I have by now is...
2
by: Iyer, Prasad C | last post by:
Actually I am bit confused between the modules and .py file How do I differentiate between the 2. For example I have a file import1.py, import2.py file Which has few functions and classes And...
8
by: Jeremy Moles | last post by:
So, here is my relevant code: PyArg_ParseTuple(args, "O!", &PyType_vector3d, &arg1) And here ismy error message: argument 1 must be pylf.core.vector3d, not pylf.core.vector3d I know...
7
by: Lauren Quantrell | last post by:
At running the risk of asking how big is too big... Is there a rule of thumb or a best practice that says I may have too many modules? I currently have a Access2K app with about 30 code modules,...
22
by: KvS | last post by:
Hi all, to start with, excuse me, I'm still learning programming alltogether, probably I'm making some fundamental mistake here... I have the files settings.py, GUIclasses.py and main.py in...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.