4 ms·
One case I know of where eval is used extensively is Python's namedtuple function from the collections module. It generates a class that behaves like a tuple, e
by anon4 12y ago
One case I know of where eval is used extensively is Python's namedtuple function from the collections module. It generates a class that behaves like a tuple, except the positions are named, so kind of like a read-only struct. Python has the __slots__ facility for exactly this situation - when you know exactly how many members your instances will have and don't want extensibility, but when namedtuple was written, it wasn't possible to programatically generate a class body with __slots__ in any way other than to generate the source for it and call eval. So here we are.
- kazinator 12y agoIndeed, some things can't be done unless you use eval; but some of those things just need something much less than eval added to the language and then they can be done. Case in point: if you don't have apply in Lisp, how do you take a list, and turn it into the arguments of a function call? The ability to do this is actually represents a modicum of introspection, but far short of a full blown eval.
- deleted 12y ago[deleted]
- GregBuchholz 12y ago;;; The Daily WTF to the rescue, no eval or introspection needed (defun my-apply (f lst) (case (length lst) (0 (funcall f)) (1 (funcall f (nth 0 lst))) (2 (funcall f (nth 0 lst) (nth 1 lst))) (3 (funcall f (nth 0 lst) (nth 1 lst) (nth 2 lst))) (t 'YouNeedToExtendThisInTheObviousWay))) ...IIRC Common Lisp allows your implementation to limit the number of arguments a function can take. http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/convar_call-a_uments-limit.html http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec...