quick eval question

Jason Orendorff jason at jorendorff.com
Wed Dec 19 17:15:49 EST 2001


> # now i want a 'path component'
> ePath ="['OUTLOOK']['CREATEDATE']"
>
> # i can't seem to put the dict and path together
> print elements+ePath
> #print elements eval(ePath)
> #print eval(elements eval(ePath))
>
> hope this makes sense,

Yep, it makes sense.  But you don't want to use eval for this.
It's slow and you're likely to make mistakes with it.  Try something
like this instead:

  name = 'OUTLOOK'
  print elements[name]

Once you understand that, try this one:

  def follow_path(object, path):
      result = object
      for name in path.split():
          result = result[name]
      return result

  path = "OUTLOOK CREATEDATE"
  print follow_path(elements, path)

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list