Linking Dictionary Values

Corrado Gioannini gioco at nekhem.com
Fri Aug 24 11:12:45 EDT 2001


On Fri, Aug 24, 2001 at 09:31:44AM -0500, Maan M. Hamze wrote:
> Is it possible to link Dictionary values of each key into a text file?  So
> the value of a key is a text file somewhere on the PC.  And if so, how do I
> instruct Python to open the text file and print into screen when the key of
> the text file is used?
you can do it by just putting the file name in the dictionary and define a
function to print its content:

dict = {'key1':'/path/file1', 'key2':'/path/file2'}

def printFile(filename):
    f = open(filename)
    print f.read()
    f.close()

and then just call:

printFile(dict['key2'])

> Also, is it possible to link a value of a Dictionary key into a Python
> function/procedure?  So, if the key is encountered, a way for the
> functionprocedure to be run.

yes, just do it:

def exampleFunction():
    print 'something'

dict = {'key': exampleFunction}

then you can for example:

apply(dict['key'])



cheers
Corrado

-- 

Corrado Gioannini
<gioco at nekhem.com>

"Thought is only a flash between two long nights,
                                         but this flash is everything."
                                                          (H. Poincaré)
		      




More information about the Python-list mailing list