How to execute arbitrary code in Python?

Asked

Viewed 266 times

4

I use an xbase language that has macro substitution as in the original Clipper. Is there anything equivalent in Python? We use a lot of command line assembly via menus at runtime. For example, making filters.

  • would that be something? [http://dbfpy.sourceforge.net/]&#Xa/

  • This question got me back to the beginning of my career :). Both Python and php have Eval. But it is good to be careful because the processing may be different from the way we did in xbase. There is already a section in the SE for vi fans (another veteran), who knows would not be an opportunity to do one for xbase/Clipper/Foxpro?

  • Did the posted answer solve your problem? Do you think you can accept it? If you don’t know how to do it, check out [tour]. This would help a lot to indicate that the solution presented was useful to you and to give an indication that it is satisfactory. You can also vote on any and all questions or answers you find useful on the entire site. Accepting and voting are separate things.

1 answer

3

I have great experience with Clipper, I worked in the world’s largest user of this language who had privileged access to the language provider and participated in the group of Harbour developers, the Clipper replacement.

I can tell you that if you used macro, you used language wrong. You can do everything without it and it’s always better to do it like this (unless you wanted me to compile something in Runtime, what only Harbour has something better, the Clipper didn’t have).

In all languages this kind of thing is problematic and not recommended. As far as it would be interesting to use (I can only think of one case), the right way is to do something more appropriate (even if it is more complex). There are better solutions, even for the cases mentioned in the question. Macro was a creation of dBase II because there was no programming language (even had array) and that was the way to solve some things. dBase III has already given better solutions to most issues without using macro. The Clipper, especially the 5, solved almost every other case, and Harbour killed the need for macro.

If you want to make the same mistake in Python, the function eval() is what you’re looking for. But I stress that she’s inherently insecure and unnecessary. Of course, if you know what you’re doing, you can even use it and you can add the security you need before you use it, but it’s hard to use right, in general it’s worth doing otherwise.

Related: Eval is either good or bad?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.