[Tutor] dictionary and more

Alan Gauld alan.gauld at btinternet.com
Mon Oct 11 22:57:00 CEST 2010

"Jack Uretsky" <jlu at hep.anl.gov> wrote 

Please do not reply to an existing thread when starting a new topic. 
This messes up threaded newsreaders and several archives. 
Start a new topic with a newmessage.

>  I apologize for the size of the attached file, but the question 
> would make little sense if I made substantial deletions.
>  The program gives the following error:
> Traceback (most recent call last):
>   File "/Javastuff/python/Glau_Is.py", line 131, in <module>
>     Adestin[state]()
> TypeError: 'NoneType' object is not callable

This says that Adestin[state] is None. 
So lets see how Adestin gets initialised....
Its a function call, which returns None. So Python is correct.
You want to store the function names (more accurately the 
function objects) so you do not want the () after the name 
when you initialise the dictionary.

Here is an example:

def f1(): print 'f1'

def f2(): print 'f2'

dct = {'1': f1, '2': f2}  # notice no parens, only a reference

for key in dct:
    dct[key]()     # use the parens here to call the func


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list