Hello everyone.
I want to learn if there is a direct way of changing strings into “source-text” -unquoted text in python files-. If not -I suppose so-, that would probably be good to add a new keyword, maybe raw. That will say Python interpreter to execute the string -which comes after this keyword- as source text. Not as a string.
I -there may also be others- sometimes really need to change strings into source text for some reason. I have listed some of my basic reasons below:
- (main reason) To put an input(not just user-input) directly inside the code
- To create custom-named variables/functions/classes
- To add code into a running module(not permanently)
I want to give an example about it:
module_name = input("Enter a module name to import: ")
func_name = input(f"Enter a function name exists in {module_name}: ")
args = input("Enter some arguments to pass to the function: ")
import raw module_name
func_call = module_name+"."+func_name+"("+args+")"
print("Result of the process: ", raw func_call)
As a sample usage of this simple code, I prepared this for you to have a look at:
Enter a module name to import: re
Enter a function name exists in re: findall
Enter some arguments to pass to the function: "dolor","Lorem ipsum dolor sit amet..."
Result of the process: ['dolor']
.
.
One more example:
new_line = input("Please enter a code line: ")
raw new_line
Sample about its usage:
Please enter a code line: print("Hello World!")
Hello World!
.
.
I don’t think that we are able to do such kind of things currently.(If we are, please let me know about it ). This keyword may bring a bit more ease to Python and I hope you will take this post in account. Thanks for reading.