This question already has an answer here:
Nested arguments not compiling
(1 answer)
Closed 7 years ago.
I have a piece of code that looks completely fine,
def _change_id(self, model, path, it,(old_id, new_id)):
But whenever I try to run it in my terminal python returns, "SyntaxError: invalid syntax"
The use of the tuple parameter was removed in python 3.0. This caused more issues than it was worth. You can rewrite it this way:
def fun(p1, b_c, p2):
b, c = b_c
the parameter b_c was a tuple:
fun(1, (1, 2), 3)
Its called Removal of Tuple Parameter Unpacking (only in python3)
see http://legacy.python.org/dev/peps/pep-3113/
Apparently, tupple parameter unpacking was removed in python 3 as per this link here
Edit: #Yoav and #jonrsharpe beat me to it
Related
This question already has answers here:
Long Int literal - Invalid Syntax?
(2 answers)
Closed 2 years ago.
I'm trying to convert a small file from Python2 to Python3.
This hex variable assignment is blocking me.
a = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
The error I'm receiving is:
a = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
^
SyntaxError: invalid syntax
Any suggestions?
Is there a problem if you remove the L? Is that not the value you expect?
>>> 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
115792089237316195423570985008687907853269984665640564039457584007908834671663
Wolfram alpha thinks that's the correct value!
This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
Closed 2 years ago.
I've run into this unseen list object, when I tried to play with list append, and I've searched hard but unable to find much information. So this is what's happening:
L = ['dinosaur']
L.append(('theropoda', L))
print(L)
# ['dinosaur', ('theropoda', [...])]
Questions - what's the [...] means here? Thanks.
As mentioned in the comments, Python will not attempt to include a circular/recursive reference in the representation of a list.
It would appear that the __repr__ function (also used by lists to create the string for printing) is implemented via reprlib with recursive support. Without it, you would end up with a RecursionError as to output the list, Python must include the nested version of the list, which also needs the nested version, and so on. Instead, it outputs the special value of ... which indicates to you that it is a recursive reference.
This question already has answers here:
Why can't non-default arguments follow default arguments?
(4 answers)
Closed 2 years ago.
Actually i am a newbie to the programming world and i have defined a very simple function that will just add three numbers.
Here's the code:
def sum(a=2,b,c):
print(a+b+c)
sum(b=1,c=3)
But it just returns me an error: SyntaxError: non-default argument follows default argument
It would be great help if someone could explain this error. Thanks.
You cannot have default arguments (a=2) before positional arguments (b, c). Fix this by putting a at the back of the signature (or change c to be the default argument).
def sum(a, b, c=2):
This is because it can make calls very confusing. For instance, let's say you have this signature:
def sum(a, b=2, c, d=4):
What happens when the user calls
sum(1, 2, 3)
Do they mean sum(a=1, b=2, c=3), or sum(a=1, c=2, d=3)?
This question already has answers here:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied
(2 answers)
Closed 4 years ago.
I am getting this error --
Incorrect number of bindings supplied The current statement uses 4,
and there are 3 supplied.
Here is the code --
def update_entry(self):
self.cur.execute('UPDATE SINGLE SET IN=?,OUT=?,QUALITY=? WHERE ID=?',
(self.IN_entry.get(),self.OUT_entry.get(),self.QC_entry.get()))
You have 4 placeholders in your query and yet you're supplying only 3 items in the tuple passed to execute. You should supply a value for the placeholder for ID as the 4th item in the tuple.
i=3,b=5
Why can't I run the above line in python? It says can't assign to literals error. It is not an error in Java or C++.
The big-picture answer to "why not?" is that the designer of the language was not fond of that syntax and preferred that programmers write
i, b = 3, 5
The technical reason why this is not allowed, and the explanation for your error message, can be found by reading the official grammar.
You can also experiment to see what the meaning of your phrase would be by inspecting the Python AST. Run this:
import ast
print(ast.dump(ast.parse("i=3,b")))
You get
Module(body=[Assign(targets=[Name(id='i', ctx=Store())], value=Tuple(elts=[Num(n=3), Name(id='b', ctx=Load())], ctx=Load()))])
So just writing i=3,b is an assignment of the tuple (3,b) to the variable i. Now you can write
i = b = 3
which assigns 3 to both i and b. But if you wrote
i = 3, b = 5
Then you would be trying to assign 5 to the tuple (3,b) which is not allowed.
This is the way Python is. The designer of Python wanted it to be that way. And he is the boss.