Is Eval *always* Evil?

Christian Heimes lists at cheimes.de
Thu Nov 11 00:14:36 CET 2010


Am 10.11.2010 18:56, schrieb Simon Mullis:

Yes, eval is evil, may lead to security issues and it's unnecessary
slow, too.

> # In the meantime - and as a proof of concept - I'm using a dict instead.
> 
> xpathlib = {
>         "houses"            : r'[ y.tag for y in x.xpath("//houses/*") ]',
>         "names"             : r'[ y.text for y in x.xpath("//houses/name") ]',
>         "footwear_type"   : r'[ y.tag for y in
> x.xpath("//cupboard/bottom_shelf/*") ]',
>         "shoes"              : r'[ y.text for y in
> x.xpath("//cupboard/bottom_shelf/shoes/*") ]',
>         "interface_types" : r'[ y.text[:2] for y in
> x.xpath("//interface/name") ]',
>  }

You have two possibilities here:

 * either learn more XPath. You can do everything with XPath as well,
for example "//houses/name/text()"

 * use lambdas instead, for example "names" : lambda x: [y.text for y in
x.xpath("//houses/name")]

Christian




More information about the Python-list mailing list