Why am I getting errors with execs nested in functions? [duplicate] - python

This question already has answers here:
Why doesn't exec work in a function with a subfunction?
(6 answers)
Closed 7 years ago.
I'm trying to make a "compiler" for my game (so that people could do intresting stuff but not inject code), for mainly declaritive "code" (It would look like this: {"player_location":"IceHall.A7", "print", "You are teleported somewhere", "tiles":{"FirePlace.B3":{'Type':"Corner", "Actions+":{....}}}}. This is how a action is represented; It is called when the player does it.
Anyways, it would have to be compiled into a function. When I tryed out something similar in the interactive interpreter (specifically:
def compile(code):
def act():
exec code
return act
). This (which is would be more or less what would be in the final, with the exception of "code" being constructed by me) raised a odd error:
File "", line 3 SyntaxError:
unqualified exec is not allowed in
function 'act' it is a nested
function.
How do I get around this?

The answer, as said in this question, is the lack of context. I wanted exec code in locals(), globals()

Related

Is it possible to declare a function called 'except' in Python? [duplicate]

This question already has answers here:
Is it possible to escape a reserved word in Python?
(5 answers)
Closed 2 years ago.
Currently I am trying to implement an API in Python that originally was written in another programming language. This API has a function called except. I am trying to implement this function in Python but obviously this resulted in an error as except is already part of the Python language. It's probably bad practice, but does someone know if it's even possible to declare a function called except?
def except():
print('hi')
>>>
def except():
^
SyntaxError: invalid syntax
It's not possible; except is a Python keyword and cannot be used as a user-defined name in any way. You can find the complete list of keywords through the keyword module.
No it is a reserved keywords. They can't be used to declare a variable or a function.
You can't declare a method named "except" cause "except" is a reserved keyword in python but you can use "Except"

Simply python script is giving error, what is wrong? [duplicate]

This question already has answers here:
How do I forward-declare a function to avoid `NameError`s for functions defined later?
(17 answers)
Closed 3 years ago.
Simple python script is giving error, what is wrong?
var ="first variable"
myfun(var)
def myfun(var):
print(var)
Error -> NameError: name 'myfun' is not defined
This thing is quite obvious though. Python reads the code line by line and not like C.
Just switch your two blocks i.e. definition of function and calling it.
var ="first variable"
def myfun(var):
print(var)
myfun(var)
This should be good to go.
When python interpreter sees the statement myfun(var), the name myfun is not defined yet. You need to move this line after your function definition.

python - Variable scope after using a 'with' statement [duplicate]

This question already has answers here:
Scope of variable within "with" statement?
(2 answers)
Closed 3 years ago.
I didn't find the answer for this question on stackoverflow, so I thought it might be helpful to ask it, and have it here -
I am declaring a new dictionary after I open a file, in the following way -
with open('some_file.txt','r') as f:
dict = json.loads(f.read()) #converts text to a dictionary
my question is - will I be able to reach dict content's even after the 'with' scope ends.
Thanks
Yes, in Python the scope of a variable ends only when the code block it's defined in ends, and the with statement is not a code block per the documentation:
The following are blocks: a module, a function body, and a class
definition. Each command typed interactively is a block. A script file
(a file given as standard input to the interpreter or specified as a
command line argument to the interpreter) is a code block. A script
command (a command specified on the interpreter command line with the
ā€˜-cā€™ option) is a code block. The string argument passed to the
built-in functions eval() and exec() is a code block.
In python scope is defined by functions. There is no indentation scope (similar to "bracket" scope in other languages). The with part affects just the f object.
Yes you will, you will not be able to access f, everything else is fair game.

how to use print both as a function and a variable in one program [duplicate]

This question already has answers here:
Why does code like `str = str(...)` cause a TypeError, but only the second time?
(20 answers)
Closed 6 years ago.
I have used print to store value like:
print=3
After then I am not able to use it to print any message:
print('a message')
Its giving error:
'int object is not callable'
Is there any way to use print both as a variable and a functions? If not then Why not python just makes built-in function names as keyword to remove this conflict?
Functions and data share the same namespace in Python -- as they do in many other languages (the entire family of LISP-1s comes to mind first, including Scheme and Clojure; also Ruby, Groovy, and I'm sure many more).
Thus no, you cannot do this. Widely available checkers (pylint, pychecker, etc) will catch and report on attempts to shadow builtins (such as print) with data.

What does the term pass mean in python? [duplicate]

This question already has answers here:
How to use "pass" statement?
(18 answers)
Closed 6 years ago.
What does pass mean in python? I have seen it used and I do not see why you need to use it or what it does? I guess I could say it passes over whatever function it is in, but why include it?
Quoth the doc:
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
References:
https://docs.python.org/2/reference/simple_stmts.html#the-pass-statement
https://docs.python.org/2/tutorial/controlflow.html#pass-statements
"It is used when a statement is required syntactically but you do not want any command or code to execute.
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):"
https://www.tutorialspoint.com/python/python_pass_statement.htm

Categories

Resources