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

global variables extended to other objects/modules

KN

Hello, I'm new to this list, and I have an important
question at the beginning.

There is a matter that concerns me - it's the globales
implementation. I've read nearly everything I found
about this and about problems people were having with
using it.

And now I'd like to ask here, because it seems to be
more than globales() matter, but an object-programming
one. I don't know any patterns for solving such problems.
Actually I'm working on an web application based on
mod_python, but this is not very important here. I use
MVC patterns for application flow, so I have one object
that works as handler and controller, and this object
creates Action-classes which run all necessary business
logic by creating other objects that for example fetch
information from database. You probably know what I need
to do here - I need a link to the database that will be
accessible from _every_ module and object that needs to
use it.

I wanted it to be started and closed by main module -
controller.py. Main area where I will use it will be
a bunch of small model-objects, like Person/Store etc.
classess that fetch data from db, saves them, return
them to Action object which passess them back to the
controller etc.

Now, when I cannot have a really global attributes that
will be accessible even for objects imported from
different modules, only way I see is to pass it in
constructors, so:

controller.py -> Action.__init__(dblink)

then in Action I need to use Person and Job classess
that both need db access:

action.py -> Person.__init__(dblink), Job.__init__(dblink)

and so on.

Is this a common way of solving this problem? What I
don't want for sure is multiple connections per request
so I need a single object storing db_link. Is there
any solution that will not force me to pass this reference
to every object I create, when I need db access?

I thought something about using globals() there and I
found that I can use sys.modules for getting the main
object (controller) from any other module.

when I define in controller.py which is __main__
something like global db = DBlink()

I can use in any module something like this, right?

local_db_link = sys.modules['__main__'].db

and I will get reference to this link created on the
beginning of application?

But this seems to be ugly and not very object-oriented?

The problem wouldn't be so big if I hadn't so many
attributes I need to pass to most of my objects, like
request, session, configuration and other attribs that
should be 'application-wide'
It would be very bad to define in every object __init__
which will have so many arguments.

Best regards,
Kamil

Jul 18 '05 #1
1 1611
KN <va**@dead.art.pl> writes:
Hello, I'm new to this list, and I have an important
question at the beginning.

There is a matter that concerns me - it's the globales
implementation. I've read nearly everything I found
about this and about problems people were having with
using it.


Another poster made several good and I would say "correct"
suggestions.

Here's another for you to consider. At the risk of making your code
unreadable and possibly buggy;

Just "stick those pesky globals right down where you need em!

Suppose you have a module named A which imports B and B imports C...

From A, it is possible to say B.C.var = something

This works but is a nasty hack that should be saved only for extreme
emergencies!

Peace

--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Jul 18 '05 #2

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...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
8
by: newbie | last post by:
Hello, I have questions about global variables in OOP (in general) and Python (in specific). I understand (I think) that global variables are generally not a good idea. However, if there are...
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.