Use of eval in Python

eval and exec are handy quick-and-dirty way to get some source code dynamically, maybe munge it a bit, and then execute it — but they’re hardly ever the best way, especially in production code as opposed to “quick-and-dirty” prototypes &c. For example, if I had to deal with such dynamic Python sources, I’d reach for … Read more

Is there C/C++ equivalent of eval(“function(arg1, arg2)”)?

C++ doesn’t have reflection so you must hack it, i. e.: #include <iostream> #include <map> #include <string> #include <functional> void foo() { std::cout << “foo()”; } void boo() { std::cout << “boo()”; } void too() { std::cout << “too()”; } void goo() { std::cout << “goo()”; } int main() { std::map<std::string, std::function<void()>> functions; functions[“foo”] = … Read more

What is the best way to handle exceptions in Perl?

The consensus of the Perl community seems to be that Try::Tiny is the preferred way of doing exception handling. The “lenient policy” you refer to is probably due to a combination of: Perl not being a fully object-oriented language. (e.g. in contrast to Java where you can’t avoid dealing with exceptions.) The background of many … Read more

How to pass arguments to the __code__ of a function?

I am completely against this use of __code__. Although I am a curious person, and this is what someone theoretically could do: code # This is your code object that you want to execute def new_func(eggs): pass new_func.__code__ = code new_func(‘eggs’) Again, I never want to see this used, ever. You might want to look … Read more

tech