Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 29th, 2006, 8:58 AM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile Update a Module

Is there a way to update a variable in a module from the program that imported this module? So other programs importing this module later can also use the update.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 29th, 2006, 9:13 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Make the variable stored in a data file? Then the module will open the data file everytime it is loaded/called to use that variable?
Sane is offline   Reply With Quote
Old Apr 29th, 2006, 1:19 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
You can use the shelve or pickle modules to maintain persistant variables. The __file__ variable contains the filepath of the currently executing module. Combine the two, and you could have something like:
import shelve
import os

# Open a shelve file in the same directory as the module
filename = os.path.join(os.path.dirname(__file__), "data.shelf")
shelf = shelve.open(filename)

# Store a variable in the file:
shelf["something"] = 10

# Retrieve a variable from the file:
print shelf["foobar"]

# Delete a variable
del shelf["old"]
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC