Re: converting dict to object

2006-12-02 Thread Neil Cerutti
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']

Re: converting dict to object

2006-12-02 Thread Carl D. Roth
On Fri, 01 Dec 2006 17:48:40 -0800, rieh25 wrote: If I have a dictionary such as: d = {'a' : 1, 'b' : 2} is there a way to convert it into an object o, such as: o.a = 1 o.b = 2 Rather, the question could be asked the other way around: how can you convert that object into a dict? The

Re: converting dict to object

2006-12-02 Thread John Machin
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...

Re: converting dict to object

2006-12-02 Thread Fredrik Lundh
John Machin wrote: Any experiences of keyword-bite? creating or manipulating CSS-styled (X)HTML using an XML binding that exposes XML attributes as Python attributes. (this could be viewed as an unnecessary restriction in the Python parser; it wouldn't be too hard to allow reserved words for

Re: converting dict to object

2006-12-02 Thread Steven D'Aprano
to converting the dict into object attributes. Why not just delegate to a saved copy of the dict? class Spam: def __init__(self, d): self._d = d def __getitem__(self, key): return self._d[key] def __setitem__(self, key, value): self._d[key] = value s = Spam({a: 1

Re: converting dict to object

2006-12-02 Thread John Machin
Steven D'Aprano wrote: On Sat, 02 Dec 2006 12:16:24 -0800, John Machin wrote: 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). [snip] But if he's doing something

Re: converting dict to object

2006-12-02 Thread Steven D'Aprano
On Sat, 02 Dec 2006 17:00:15 -0800, John Machin wrote: Steven D'Aprano wrote: On Sat, 02 Dec 2006 12:16:24 -0800, John Machin wrote: 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

Re: converting dict to object

2006-12-02 Thread Neil Cerutti
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',

Re: converting dict to object

2006-12-02 Thread John Machin
Neil Cerutti wrote: Thanks for the pointer to keyword module. I hadn't noticed it yet. Bonus: you got an extremely fresh, scarcely used pointer -- I wasn't aware of it myself till today :-) Cheers, John -- http://mail.python.org/mailman/listinfo/python-list

converting dict to object

2006-12-01 Thread rieh25
If I have a dictionary such as: d = {'a' : 1, 'b' : 2} is there a way to convert it into an object o, such as: o.a = 1 o.b = 2 thanks -- View this message in context: http://www.nabble.com/converting-dict-to-object-tf2741429.html#a7649225 Sent from the Python - python-list mailing list

Re: converting dict to object

2006-12-01 Thread Gabriel Genellina
At Friday 1/12/2006 22:48, rieh25 wrote: If I have a dictionary such as: d = {'a' : 1, 'b' : 2} is there a way to convert it into an object o, such as: o.a = 1 o.b = 2 class X(object): ... def __init__(self, d): self.__dict__.update(d) ... d = {'a' : 1, 'b' : 2} o=X(d) o.a 1 o.b 2

Re: converting dict to object

2006-12-01 Thread Michel Claveau
Hi! Yes. But... Try:d = {'a': 1, 'b': 2, 'def': 123} Ok, I go out... -- @-salutations Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list