This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 9 years ago.
I want to do following. Script has some python code as a string (saved in a variable) and is it possible to run that code ?
Well, I know one way, writing that string to a file & running it, but I don't want that. Without creating any extra file, is it possible to run it ?
Here is a example :
let's assume my python file has following content
#this is a main python file
content = ''' print 'hello!'
print 'this is from sub python code' '''
print 'from main python'
The content string has a python code & I want to run it. Is it possible ?
Hope I am clear. Thank you !
I'll say this up front: This is a terrible idea, and depending on the source of the string a serious security risk.
That disclaimer out of the way, python has an exec function that executes a string containing python code. For example:
exec("print 2+2")
Edit: I originally used eval in my answer, which is useful for evaluating individual expressions, while exec can be used for more general execution of arbitrary python code in a string.
Relevant docs:
http://docs.python.org/2/reference/simple_stmts.html#exec
http://docs.python.org/2/library/functions.html#eval
Well you could use eval:
eval(content)
And that will do what you want, however it's not recommended, especially if someone else controls the content of content - it's not too hard to hack into your system if you have eval
Did you tried with exec method as per documentation that should do
exec "print 'Hello, World!'"
Depending on the code you are trying to execute, you may use eval() or exec. There are several differences between these two options:
eval() does what it should: it evaluates an expression and returns a value, not executes code. That means you may call functions, do some arithmetic, even use list comprehensions, generators or lambdas, but not execute python statements that aren't expressions (e.g. if, for, print in Python 2; however, in Python 3 print is a function and is ok).
eval() accepts more parameters than just a string. It gets locals and globals, two dictionaries, defining the scope environment. You may make evaluation nearly (though not really) safe for untrusted strings if you fill and pass these dictionaries to eval(). Probably, you may even redefine builtins by properly setting __builtins__ in globals. http://docs.python.org/2/library/functions.html#eval
exec also accepts globals and locals. See http://docs.python.org/2/reference/simple_stmts.html#exec . And it may execute everything. And it is virtually impossible to make it even relatively safe.
Related
I would like to call a function in Python like a keyword. For motivation I have following problem:
Multiple variables can be deleted at once by using the keyword del.
x1,x2=1,1
del x1,x2
However,
x1,x2=1,1
del x1,x2,x3
leads to a name error if x3 is not defined. The convenience function Del deletes multiple variables independently of their existence (see this SE post):
def Del(*d:list[str])->None:
for i in d:globals().pop(i,None)
I can now call
x1,x2=1,1
Del('x1','x2','x3')
without getting an error message about non-existence of x3. However, for my new command Del I have to use brackets and quotes whereas for del I don't need them. The reason is that Del is a function, whereas del is a keyword.
How could I define Del as a keyword to call it like Del x1,x2,x3? Of course, any other method that saves quotes or brackets is welcome.
You cannot extend the grammar of Python via Python code.
Python is a mix between an interpreted and compiled language. This means that a process or program must convert the source code into another form before it can be executed. It is this process that ultimately understands the grammar that makes up Python (including all of the keywords, statements, and other syntax).
In order to extend or change the grammar, you need to change/modify the source code of that process. This is possible, but is not something that would be easy to do (you would have to modify the C code from which the Python binary is built). Additionally, even if you were successful, you could only use the new grammar for programs run using your custom binary. Anyone else running your code would receive syntax errors.
I am trying to translate this from Bash to Python:
password=$(func_name "${configFile}" "<password" "2")
func_name and configFile have been defined earlier in the script. func_name is a function and configFile is a working directory leading to an XML file.
But I don’t know what to do with func_name in this case.
And is password and array here?
Knowing that an array in Bash is called a list in Python, I have tried this, but I am not sure:
password = [configFile, "<password", "2"]
Is this correct?
A rough translation would be:
password = func_name(configFile, "<password", "2")
But this won't necessarily work at all. Python and bash think in fundamentally different ways, and you can't really "translate" back and forth between them; you have to think differently in the two languages.
For example, bash functions don't really have return values. They can print output as they run (the output being a sequence of bytes), and return a status code (basically, whether the function succeeded or not). The bash code you have captures the output (what the function prints), treats it as a string, and stores it in the password variable.
Python functions return objects. bash has no concept of an object. Python objects can be strings... or any of a variety of built-in object types, or any type you import from a library or define yourself. My Python code here takes whatever object the function returns, and stores it in the password variable. BTW, Python functions don't have return statuses, instead they can throw errors (which is a concept bash doesn't have).
Similarly, the arguments you pass to a bash function are all strings, but in Python they're objects (which can be strings, but can also be completely different types of things).
I would strongly recommend learning the languages you're trying to use. You can't translate between them at the syntactic level, you need to translate at the conceptual level, and to do that you need to understand both languages at that level.
I have a question regarding python I wrote a code in python shell that shows different output and python IDLE shows different output for the same piece of code
I try to write the same code twice on python shell then tried in IDLE.
Python v3.7
a="aster\n"
b="aster\n"
print(id(a))
print(id(b))
I expected the output should be the same for both the print statement
It's an implementation detail.
In the interactive interpreter used by IDLE, each line is parsed and compiled separately, and it isn't bothering to check for b if a str object equal to 'aster\n' is already allocated, so you get two distinct objects.
If you put this in a script, the entire script is parsed before the compiler uses the resulting AST to generate code. By parsing everything at once, the compiler can notice that the same immutable string is used in more than once place, which allows it to generate code that uses multiple references to the same underlying str object.
In general, you should only care about the output of id or the result of an is comparison if you assigned the value from one name to another your self; don't assume that two literals that look the same will share a single underlying object.
Help on built-in function id in module builtins:
id(obj, /)
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
(CPython uses the object's memory address.)
As you can see id returns the memory address of the object. So even you run the same script you will get different addresses.
In R, when you assign something to a variable, you can wrap your code in brackets to also print the result:
> a<-1+2
> a
3
>(a<-1+2)
3
Is there an equivalent feature in python?
No. You can't mix statements and expressions in the same "command".
You can, however, use ; to have both on the same "line":
a = 1 ; print(a)
# 1
There is no single statement which allows to that.
First we have assignment statements. Which allow to, well, make assignments.
And then you have either a print function (for Python 3.x) or a print statement (for Python 2.x). But neither of them cannot be mixed with the aforementioned assignemnt statement.
So no, you cannot do that in a single statement. You can, of couse, cheat that into one line by using ; but that is something not very readable which I would not recommend.
Bonun non-aswering rambling
From a readability point of view, assigning and printing are two very different things which should not be mixed.
Moreover, if you are (for instance) using a logging library instead of direct print usage, then the "single statement" you are looking for would become useless. In fact, I think that that is a rationale behind moving print from statement to function.
So, you don't have a single statement for assign-and-print and, I will add, that kind of statement doesn't seem a good idea in the Python language.
I try to write my first python program. But the print function is very special. I tried to write :
print ("success!")
very normal. but IDLE remind me that the code is wrong and "print" has been red. What should I do?
I think you are just seeing IDLE's syntax-highlighting feature. Keywords (and function names, variables etc.) are given different colours, in order to make the code easier to read (and spot errors like missing brackets etc.):
If you are seeing the same as this:
Then, you have nothing to worry about
Assuming this isn't just IDLE's syntax highlighting (hard to tell without the code in question)...
The reason for this odd behaviour has to do with a major change that was made in Python 3. In Python 2, print is a keyword. In Python 3, print is a function.
This has a number of implications:
In Python3, print requires parentheses
In Python2, print does not require parentheses by default
In Python2, print cannot be overridden by default (you cannot change what print does)
In Python3, print can be shadowed in a module, providing different behaviour for one application or another.
Now, what this means is that the syntax by default is different, but the good news is you can get the same syntax on both by putting as the first line in your module:
from __future__ import print_function
As a side note, this is a good idea since it ensures the same code will run on Python 2.7 and 3. So I add to all my Python modules these days:
from __future__ import print_function, division
(adding division there is good practice because of the way the division operator changed.)
the problem could be anywhere in the line that print appears in check for example if you have the right indention