[Tutor] problem calling a function

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Nov 14 07:52:32 CET 2005



> eval() changes the type from string to tuple. But, to my surprise
> tuple() does not yeild the tuple that is represented by the string but a
> tuple representation of the string.
>
> If eval() does convert correctly why not use it? The string will always
> be a valid tuple.

Hi Vincent,

Alternative question: can your program just pass the tuple itself to the
function, or does it always have to turn things into strings?  Your code
in:

   http://mail.python.org/pipermail/tutor/2005-November/043208.html

constructs a string that represents a tree near the bottom of the code.
But can you modify it to construct the data structure that represents that
tree?  I haven't read the code too closely yet, but it looks like it's
doing too much working in building a string representation.


Take a look again at Roel Schroven's code in:

    http://mail.python.org/pipermail/tutor/2005-November/043214.html

Does the approach there make sense to you?  If that code is complex, we
could probably simplify that code to expose the core idea more clearly.
The core idea is to use real, enriched data structures: you don't have to
treat everything as an anemic string.


eval() is a dangerous function and almost always never appropriate as a
data-structure parser.  It does much more than just value conversion.
We've written about this a week ago:

    http://mail.python.org/pipermail/tutor/2005-November/042838.html
    http://mail.python.org/pipermail/tutor/2005-November/042854.html

so eva() is obviously the seductive solution, but it's wrong here.
*grin*



Hope this helps!




More information about the Tutor mailing list