List comprehension for testing **params

Cantabile cantabile.03 at wanadoo.fr
Sun Nov 11 17:24:14 EST 2012


Hi,
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)
 blah
 blah
 required = ['Subject', 'From', 'To', 'msg']
 for i in required:
 if not i in params.keys():
 print "Error, \'%s\' argument is missing" %i
 exit(1)
 ...
md = {'Subject' : 'Test', 'From' :'Me', 'To' :['You', 'Them'], 'msg' 
:my.txt"}

m = Mail('smtp.myprovider.com', ["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 :)
Cheers,
Cantabile


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /