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"
Related
This question already has answers here:
How to restore a builtin that I overwrote by accident?
(3 answers)
Closed 2 years ago.
I am new to python, and realized that i can assign print i.e. an inbuilt function as a variable, then when i use print('hello world')this shows the exact error that i faced
I am familiar to c++ and even in that we were never allowed to use an inbuilt function as a variable name.
those were the fundamental rules for naming a variable
If python.org has issued the new version I'm sure they would have done it for a reason, bbut i want to know how do i access my print statement after assigning a value to it?
you won't be able to access your print function unless you do hacky things, which I recommend not to do them in the middle of your code.
Also it is good to know that python (as c++) has scopes for variables, and variables "die" and they are no longer accessible when scope ends. For instance:
def change_print_value():
print = 3
change_print_value()
print('Print works as expected')
It is a good practice to avoid using reserved keywords as variable names. Any IDE has the keywords highlighted, so you can easily realize when you are using a keyword where you shouldn't.
print is not part of the reserved keywords list in python. Here's a comprehensive list of reserved words.
Functions are first class objects in python, so that means they can be treated and manipulated as objects. Since print is a function (and an object), when you call print = 1, you reassign the variable print to have a value of 1, so the functionality "disappears".
This question already has answers here:
What is this odd colon behavior doing?
(2 answers)
Closed 3 years ago.
I made a typo in my code that went completely silent syntactically.
dict_args : {"arg1":1,"arg2":2,"arg3":3}
# .... Some more code
some_function(**dict_args)
# .... Some more code
If you haven't noticed it, it's the use of : instead of = when declaring the variable dict_args.
So my question is, does the python syntax : a:1, by itself, hold any meaning ? Or should it hypothetically be considered a syntax error?
PEP-526 introduced variable annotations, which provide programmers a way to add type information to variables. This allows, among other things, statements like
x: int
to indicate that there is a local variable of type int, without initializing it. In PEP-484 - Acceptable Type Hints, we can see that annotations "must be valid expressions that evaluate without raising exceptions", which your dictionary literal is.
If you look at the Python grammar itself you can see that the expr_stmt and annassign rules make the example you show legal.
If you're using an IDE/other type hinting tools, they should definitely complain about this, but it doesn't break the rules that Python has set up.
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.
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
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()