faqts : Computers : Programming : Languages : Python : Snippets : Dictionaries

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

39 of 81 people (48%) answered Yes
Recently 0 of 10 people (0%) answered Yes

Entry

How to use Dictionary look-up

Dec 7th, 2000 17:51
hasan kacmaz, aleaxit@yahoo.com;


This is what I think would help, if one wants to use a dictionary look-
up as a value within a code that requires some configuration input. For 
example, a dictionary look-up with dictionary['username'] should bring 
up the user number etc...
the syntax below should do a dict-lookup where it's called within a 
keywords-dictionary which callable can retrieve as **kw (ie. callable
(aname)) :
	class Dictionary:
	    self.dictionary=Dict_lookup(UserName)
	    def lookup_key
	        return apply(self.dictionary.lookup_key,args)
	class Dict_lookup:
	    def __init__(self,**kw):
		self.dict=kw
	    def lookup_key (self,sn):
		return self.dict[sn]
Alex Martelli has pointed out another way for the case where one 
dictionary value called in a function...
ie.
	class Dict_lookup:
	    def __init__(self,kw):
        	self.dict=kw
	self.dictionary=Dict_lookup({UserName:function(UserNumb)})
OR (for python 2.0...!)
	self.authenticate=Dict_lookup(**{UserName:function(UserNumb)})
HK