On 2006-12-02, John Machin <[EMAIL PROTECTED]> wrote: > Neil Cerutti wrote: >> On 2006-12-02, Michel Claveau <[EMAIL PROTECTED]> wrote: >> > Hi! >> > >> > Yes. >> > >> > But... >> > >> > Try: d = {'a': 1, 'b': 2, 'def': 123} >> > >> > Ok, I go out... >> >> How to convert a list of strings into a list of integers: >> >> a = ['82', '4', '16'] >> >> ai = [int(i) for i in a] >> >> Yes. >> >> But... >> >> Try: a = ['82', '4', '16', 'foo'] >> >> Ok, I go out... > > Michel was making (part of) a valid point: dictionaries have > more flexibility in choice of keys than objects have in > attribute names. More completely: > > Suppose d.keys() produces ["one", "def", "foo bar", 3, "3"] > > o.one is OK.
I made the assumption that Michael was also the original poster, and had somehow laid a clever trap. If I was wrong about that, my apologies. It's one thing to ask how to convert 'a' and 'b' to attributes, but quite another to convert arbitrary text. > The OP might consider adding code to the __init__ method to > check for cases where the dictionary key is not a string > containing a valid Python identifier (not a keyword). That raises the interesting question of what to do in that case. Just letting an error occur might be perfectly good behavior. Plus, I didn't know about... > Observation: the keyword module's iskeyword() function gives an > easy check. If one is supporting multiple versions of Python, a > more complicated (overkill?) approach might be desirable: use > the latest version of Python to generate a source file > containing the latest keywords from keyword.kwlist. Thanks for the pointer to keyword module. I hadn't noticed it yet. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list