On python syntax...
netspam at shic.co.uk
Tue Nov 11 13:52:59 CET 2003
Peter Otten <__peter__ at web.de> and others posted some interesting
Thanks very much for the various suggestions.
John: I'd thought of building a tree then calling write on it
myself... in one sense I find that approach neat, however it has a
significant disadvantage when it comes to writing long streams - it
needs the whole object allocated before I start writing - hence
introducing a potentially large latency and also, potentially,
requiring many costly memory allocations - which may be a significant
barrier to performance when used on 'longer' streams.
The "use a stack" idea is OK - in so far as it solves the problem of
matching up the write closing token (should I have many different tags
- say, with XML) but doesn't address my main concern - i.e. that I
don't want to have to write explicit code to output the close-context
marker - whatever it is.
Jp: Very neat Python 2.4 answer - I like it... but it doesn't really
address my current concerns... it's a touch too clever for me as it
relies upon details of the specific example I posted which don't hold
in my 'real-world' examples. I'm also concerned that I'd need to
allocate the whole string before writing it - which would cause high
latency and significant resource usage in a streaming environment.
Peter: This is far more like the sort of answer I'd hoped for... I
think something like wrap() might be OK - I hadn't realised I could
pass arbitrarily many arguments like that. One 'down' side to this is
that I'd have prefered not to have to define a new function for each
of my contexts, though I suppose this might not be too much of a
hardship. I'd have preferred to be able to have many different nested
contexts in a single function (to share local variables etc) - though
I guess that this isn't going to be possible. The wrapitems()
implementation is neat - I like it... but I don't think it would apply
to my real world problem - where I expect to do more complex things
than just iterate a fixed loop - I envision other "contexts" for
example: conditionals, while loops, functions etc.
I still have a feeling that some way to hook the close of an execution
context would be extremely useful... though I'm beginning to believe
that this isn't currently supported. I'd be completely satisfied with
wrap() if I could define the wrapped function inline somehow - yet
still execute it within wrap. I'd want the wrapped function to be
anonymous and to have access to all the local variables available to
wrap() - without having to explicity pass each one to the wrapped
function... I guess the obvious lambda args:... approach isn't going
to be effective because I'd still need to specify every argument
More information about the Python-list