[lxml-dev] should _setElementValue add type attributes?
jholg at gmx.de
jholg at gmx.de
Mon Aug 27 23:03:00 CEST 2007
Hi Stefan,
> jholg at gmx.de wrote:
> >>>> root = objectify.Element("root")
> >>>> root.x = "3"
> > behave differently from
> >>>> root = objectify.fromstring("""<root><x>3</x></root>""")
> >
> > Kind of losing sort of a symmetry.
>
> What bothers me more (and where I do see a symmetry) is:
>
> >>> root = objectify.fromstring("<root><flag>true</flag></root>")
>
> >>> # now
> >>> root.flag
> True
> >>> root.flag = "true"
> >>> root.flag
> True
>
> >>> # then
> >>> root.flag
> True
> >>> root.flag = "true"
> >>> root.flag
> 'true'
>
> I'm not sure what to think about that. It would be wrong to special case
> it,
> but it kinda feels wrong the way it would work in the future...
Hm, not for me (any more :). I think this is just the same case as having a literal 3 in the XML document. When parsing XML from a string or a file with no type information whatsoever, there is really only 2 things we can do:
1. Make strings of everything.
2. Use type-inference provided by the lookup mechanisms.
(1) does not make much sense as we would not really need objectify at all (except for the syntactic sugar of its __setattr__-API).
On the other hand, when setting elements by hand, i.e. in Python code, we well know the (python-)type information:
For me, it begins to rather feel more natural to do:
>>> # then
>>> root.flag = True # real live python boolean object
>>> root.flag
True
>>> root.flag.text
"true"
instead of
>>> # now
>>> root.flag = "true"
>>> root.flag
True
which is, in the end, pretty much the same as
>>> # now
>>> root.three = "3"
>>> root.three
3
So, let's go for the auto-pytype-addition in _setElementValue, without special-casing, imo.
Holger
--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger
More information about the lxml-dev
mailing list