Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python What is this feature called and what other languages have it?

Inge

New Coder
Let's say a (sanitized) user defined math expression (such as x1 + x2 * x3), with up to 9 variables xi, is my input, so i'd have:
Python:
input_str = "x1 + x2 * x3"
In python, i can define a function that executes this code, like this:
Python:
func_def_str = """
global calc_func
def calc_func(x1,x2,x3):
return x1 + x2 * x3"""

exec(func_def_str)

You can't do this in some other languages, so my questions are:
  • What is this feature called?
  • In what other languages can you do something like this? (without having to hack a compiler or something like that)
 
Last edited:
func_def_str = """
global calc_func
def calc_func(x1,x2,x3):
return x1 + x2 * x3"""

This is just a string. Can span multiple lines and be used as a doc string. No code will execute. Can print with print(func_def_str).
Can execute code inside an fstring.
Python:
x = 'Hello'

print(f'{x}')

will print Hello
 
Last edited by a moderator:
TL;DR: `exec` is a function in interpreted programming languages that execute the code passed to them. They are not found in compiled programming languages because the code has already been compiled to machine code.

`exec` is a function in Python used for dynamic execution of Python code. It should not be confused with the `eval` function in Python. `eval` in other languages like JavaScript also executes code. However, the `eval` function in Python evaluates a python expression (e.g. `1 + 1`) and returns the output.

With that out of the way, we can explain what the function does. The `exec` function is a feature confined to interpreted languages. Interpreted languages run your code line by line, which means that they can execute the code passed to an `exec` function call easily by just invoking themselves on the string of code.

Compiled languages compile your program down to native binary language (or something similar to it, like bytecode), meaning running code gained at execution time is impossible. This is because the generated executable knows nothing about the language it's code was originally written in, and would not know how to handle the string of code. There are ways to circumvent this, like writing an `exec` function to invoke a compiler that compiles the code passed to it. However, this is a process that would take time, and you would have to bundle the compiler with the program. Seeing as compilers are generally large programs, this is not practical.

All interpreted languages can (and probably do) have an equivalent of Python's `exec` function. Even if they don't it would be relatively easy to write on. Compiled programming language probably won't have one, and even if they do, it would be slow, which defeats one of the main perks of using compiled programming languages.
 
func_def_str = """
global calc_func
def calc_func(x1,x2,x3):
return x1 + x2 * x3"""

This is just a string. Can span multiple lines and be used as a doc string. No code will execute. Can print with print(func_def_str).
Can execute code inside an fstring.

Python:
x = 'Hello'

print(f'{x}')

will print Hello
Yes, everything you just said is absolutely true. But you forgot the last line
Python:
exec(func_def_str)
which is the most important on the thread. This executes the string as if it were a python file itself basically... Try it out, and after the exec line, try this:
Code:
calc_func(0, 0, 0)  # should return 0
calc_func(1, 1, 57) # should return 58
calc_func(1, 2, 3)  # should return 7
 
Java used to have the ability to execute JavaScript code from within a Java program with 2-way sharing of variables. Sadly that was deprecated in Java 11 because the maintenance effort wasn’t justified by the amount of use it got.

But now the JDK has a read/execute app called JShell that’s mainly used for checking out scraps of code interactively which is also executable from within a Java program, so you can pass it Java source code as a String and execute it on the fly.
 
VB.Net has Eval()

Also I meant that it would be extremely hard to implement that functionality in a compiled programming language, but I should have been more specific (read the last line of the last paragraph).
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom