List comprehension for testing **params

Cantabile cantabile.03 at
Sun Nov 11 23:24:14 CET 2012

I'm writing a small mail library for my own use, and at the time I'm 
testing parameters like this:

class Mail(object):
     def __init__(self, smtp, login, **params)
         required = ['Subject', 'From', 'To', 'msg']
         for i in required:
             if not i in params.keys():
             print "Error, \'%s\' argument is missing" %i

md = {'Subject' : 'Test', 'From' :'Me', 'To' :['You', 'Them'], 'msg' 

m = Mail('', ["mylogin", "mypasswd"], **md)

I'd like to do something like that instead of the 'for' loop in __init__:

assert[key for key in required if key in params.keys()]

but it doesn't work. It doesn't find anythin wrong if remove, say msg, 
from **md. I thought it should because I believed that this list 
comprehension would check that every keyword in required would have a 
match in params.keys.

Could you explain why it doesn't work and do you have any idea of how it 
could work ?

Thanks in advance :)

More information about the Python-list mailing list