PYTHON3: non-default argument follows default argument [duplicate] - python-3.x

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)?

Related

Cryptic list object resentation in Python as [...] [duplicate]

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.

How to disallow keyword arguments?

Recently I noticed that range() does not accept keyword arguments, and I thought this was a little bit strange: Even if we define no defaults we can still do
def f(a, b):
return a + b
f(a=3, b=5)
Is it therefore somehow possible to disable the use of keyword arguments in a function written in Python? Or is this just reserved for built in functions?
Since this came up as the top result for me, figured I'd go ahead and add the answer even though this is 2 years old...
def f(a, b, /):
return a + b
Any arguments before the / must be passed in as positional args.
See: https://peps.python.org/pep-0570/

When UPDATE the TABLE using python and sqlite ,, I am getting this error --Incorrect number of bindings supplied [duplicate]

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.

Strange invalid syntax Error in python [duplicate]

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

PHP Strict Standards on line with variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Annoying PHP error: “Strict Standards: Only variables should be passed by reference in”
I have this line of code,
$extension=end(explode(".", $srcName));
when I fun my function I get
PHP Strict Standards: Only variables should be passed by reference in
I am not sure how to solve this
The function end() requires a variable to be passed-by-reference and passing the return-value of a function doesn't acheive this. You'll need to use two lines to accomplish this:
$exploded = explode(".", $srcName);
$extension = end($exploded);
If you're simply trying to get a file-extension, you could also use substr() and strrpos() to do it in one line:
$extension = substr($srcName, strrpos($srcName, '.'));
Or, if you know the number of .'s that appear in the string, say it's only 1, you can use list() (but this won't work if there is a dynamic number of .'s:
list(,$extension) = explode('.', $srcName);

Resources