Catch exception “missing 1 required positional argument” if I don't send two arguments?
Is it possible to catch exception “missing 1 required positional argument” if I don't send two arguments? How to fix it if I can't change assert functions.
def sum (n,m):
return n+m
if __name__ == '__main__':
assert sum(16,15) == 31, "EXAMPLE"
assert sum([16,16]) == 32, "result - missing 1 required positional"
python
|
show 1 more comment
Is it possible to catch exception “missing 1 required positional argument” if I don't send two arguments? How to fix it if I can't change assert functions.
def sum (n,m):
return n+m
if __name__ == '__main__':
assert sum(16,15) == 31, "EXAMPLE"
assert sum([16,16]) == 32, "result - missing 1 required positional"
python
What's your question?
– user2357112
Nov 21 '18 at 23:54
2
You can catch that with a try-except, just like with any other exception, but it's not the kind of thing you should be catching in most situations. Why do you want to do that? If you're trying to write a function with a flexible signature, you should be looking at optional arguments and*args
/**kwargs
catchall arguments.
– user2357112
Nov 22 '18 at 0:11
"How to fix it if I can't change assert functions" - the way you're usingassert
makes no sense.
– user2357112
Nov 22 '18 at 0:24
The second argument toassert
is an assertion failure message, to be used if the first argument is false. It is not used if evaluating the first argument throws an exception, and it is not compared to the exception message in any way. It doesn't make sense to report an assertion failure with message "result - missing 1 required positional" ifsum([16,16]) == 32
is false.
– user2357112
Nov 22 '18 at 0:27
Ok, thank You. This is example of my exercise. I think that in this exercise is mistake, because i can't edit assert function. I can only modify my def. If i get list and i have to use function with two arguments... it's no sens. Finally, exercise like this is on the very popular page about python. (This code is short version to simply clarify problem)
– Michał Hałucha
Nov 22 '18 at 1:18
|
show 1 more comment
Is it possible to catch exception “missing 1 required positional argument” if I don't send two arguments? How to fix it if I can't change assert functions.
def sum (n,m):
return n+m
if __name__ == '__main__':
assert sum(16,15) == 31, "EXAMPLE"
assert sum([16,16]) == 32, "result - missing 1 required positional"
python
Is it possible to catch exception “missing 1 required positional argument” if I don't send two arguments? How to fix it if I can't change assert functions.
def sum (n,m):
return n+m
if __name__ == '__main__':
assert sum(16,15) == 31, "EXAMPLE"
assert sum([16,16]) == 32, "result - missing 1 required positional"
python
python
edited Nov 22 '18 at 0:16
Michał Hałucha
asked Nov 21 '18 at 23:53
Michał HałuchaMichał Hałucha
12
12
What's your question?
– user2357112
Nov 21 '18 at 23:54
2
You can catch that with a try-except, just like with any other exception, but it's not the kind of thing you should be catching in most situations. Why do you want to do that? If you're trying to write a function with a flexible signature, you should be looking at optional arguments and*args
/**kwargs
catchall arguments.
– user2357112
Nov 22 '18 at 0:11
"How to fix it if I can't change assert functions" - the way you're usingassert
makes no sense.
– user2357112
Nov 22 '18 at 0:24
The second argument toassert
is an assertion failure message, to be used if the first argument is false. It is not used if evaluating the first argument throws an exception, and it is not compared to the exception message in any way. It doesn't make sense to report an assertion failure with message "result - missing 1 required positional" ifsum([16,16]) == 32
is false.
– user2357112
Nov 22 '18 at 0:27
Ok, thank You. This is example of my exercise. I think that in this exercise is mistake, because i can't edit assert function. I can only modify my def. If i get list and i have to use function with two arguments... it's no sens. Finally, exercise like this is on the very popular page about python. (This code is short version to simply clarify problem)
– Michał Hałucha
Nov 22 '18 at 1:18
|
show 1 more comment
What's your question?
– user2357112
Nov 21 '18 at 23:54
2
You can catch that with a try-except, just like with any other exception, but it's not the kind of thing you should be catching in most situations. Why do you want to do that? If you're trying to write a function with a flexible signature, you should be looking at optional arguments and*args
/**kwargs
catchall arguments.
– user2357112
Nov 22 '18 at 0:11
"How to fix it if I can't change assert functions" - the way you're usingassert
makes no sense.
– user2357112
Nov 22 '18 at 0:24
The second argument toassert
is an assertion failure message, to be used if the first argument is false. It is not used if evaluating the first argument throws an exception, and it is not compared to the exception message in any way. It doesn't make sense to report an assertion failure with message "result - missing 1 required positional" ifsum([16,16]) == 32
is false.
– user2357112
Nov 22 '18 at 0:27
Ok, thank You. This is example of my exercise. I think that in this exercise is mistake, because i can't edit assert function. I can only modify my def. If i get list and i have to use function with two arguments... it's no sens. Finally, exercise like this is on the very popular page about python. (This code is short version to simply clarify problem)
– Michał Hałucha
Nov 22 '18 at 1:18
What's your question?
– user2357112
Nov 21 '18 at 23:54
What's your question?
– user2357112
Nov 21 '18 at 23:54
2
2
You can catch that with a try-except, just like with any other exception, but it's not the kind of thing you should be catching in most situations. Why do you want to do that? If you're trying to write a function with a flexible signature, you should be looking at optional arguments and
*args
/**kwargs
catchall arguments.– user2357112
Nov 22 '18 at 0:11
You can catch that with a try-except, just like with any other exception, but it's not the kind of thing you should be catching in most situations. Why do you want to do that? If you're trying to write a function with a flexible signature, you should be looking at optional arguments and
*args
/**kwargs
catchall arguments.– user2357112
Nov 22 '18 at 0:11
"How to fix it if I can't change assert functions" - the way you're using
assert
makes no sense.– user2357112
Nov 22 '18 at 0:24
"How to fix it if I can't change assert functions" - the way you're using
assert
makes no sense.– user2357112
Nov 22 '18 at 0:24
The second argument to
assert
is an assertion failure message, to be used if the first argument is false. It is not used if evaluating the first argument throws an exception, and it is not compared to the exception message in any way. It doesn't make sense to report an assertion failure with message "result - missing 1 required positional" if sum([16,16]) == 32
is false.– user2357112
Nov 22 '18 at 0:27
The second argument to
assert
is an assertion failure message, to be used if the first argument is false. It is not used if evaluating the first argument throws an exception, and it is not compared to the exception message in any way. It doesn't make sense to report an assertion failure with message "result - missing 1 required positional" if sum([16,16]) == 32
is false.– user2357112
Nov 22 '18 at 0:27
Ok, thank You. This is example of my exercise. I think that in this exercise is mistake, because i can't edit assert function. I can only modify my def. If i get list and i have to use function with two arguments... it's no sens. Finally, exercise like this is on the very popular page about python. (This code is short version to simply clarify problem)
– Michał Hałucha
Nov 22 '18 at 1:18
Ok, thank You. This is example of my exercise. I think that in this exercise is mistake, because i can't edit assert function. I can only modify my def. If i get list and i have to use function with two arguments... it's no sens. Finally, exercise like this is on the very popular page about python. (This code is short version to simply clarify problem)
– Michał Hałucha
Nov 22 '18 at 1:18
|
show 1 more comment
1 Answer
1
active
oldest
votes
I found the solution. I just put second argument optional. In this situation I don't have to modify my main function.
def sum (n,m=0):
if m!=0:
return n + m
elif type(n)==list:
return n[0]+n[1]
if __name__=='__main__':
assert sum(16, 15) == 31, "EXAMPLE"
assert sum([16, 16]) == 32, "result - missing 1 required positional"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422067%2fcatch-exception-missing-1-required-positional-argument-if-i-dont-send-two-arg%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I found the solution. I just put second argument optional. In this situation I don't have to modify my main function.
def sum (n,m=0):
if m!=0:
return n + m
elif type(n)==list:
return n[0]+n[1]
if __name__=='__main__':
assert sum(16, 15) == 31, "EXAMPLE"
assert sum([16, 16]) == 32, "result - missing 1 required positional"
add a comment |
I found the solution. I just put second argument optional. In this situation I don't have to modify my main function.
def sum (n,m=0):
if m!=0:
return n + m
elif type(n)==list:
return n[0]+n[1]
if __name__=='__main__':
assert sum(16, 15) == 31, "EXAMPLE"
assert sum([16, 16]) == 32, "result - missing 1 required positional"
add a comment |
I found the solution. I just put second argument optional. In this situation I don't have to modify my main function.
def sum (n,m=0):
if m!=0:
return n + m
elif type(n)==list:
return n[0]+n[1]
if __name__=='__main__':
assert sum(16, 15) == 31, "EXAMPLE"
assert sum([16, 16]) == 32, "result - missing 1 required positional"
I found the solution. I just put second argument optional. In this situation I don't have to modify my main function.
def sum (n,m=0):
if m!=0:
return n + m
elif type(n)==list:
return n[0]+n[1]
if __name__=='__main__':
assert sum(16, 15) == 31, "EXAMPLE"
assert sum([16, 16]) == 32, "result - missing 1 required positional"
answered Nov 22 '18 at 11:21
Michał HałuchaMichał Hałucha
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422067%2fcatch-exception-missing-1-required-positional-argument-if-i-dont-send-two-arg%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's your question?
– user2357112
Nov 21 '18 at 23:54
2
You can catch that with a try-except, just like with any other exception, but it's not the kind of thing you should be catching in most situations. Why do you want to do that? If you're trying to write a function with a flexible signature, you should be looking at optional arguments and
*args
/**kwargs
catchall arguments.– user2357112
Nov 22 '18 at 0:11
"How to fix it if I can't change assert functions" - the way you're using
assert
makes no sense.– user2357112
Nov 22 '18 at 0:24
The second argument to
assert
is an assertion failure message, to be used if the first argument is false. It is not used if evaluating the first argument throws an exception, and it is not compared to the exception message in any way. It doesn't make sense to report an assertion failure with message "result - missing 1 required positional" ifsum([16,16]) == 32
is false.– user2357112
Nov 22 '18 at 0:27
Ok, thank You. This is example of my exercise. I think that in this exercise is mistake, because i can't edit assert function. I can only modify my def. If i get list and i have to use function with two arguments... it's no sens. Finally, exercise like this is on the very popular page about python. (This code is short version to simply clarify problem)
– Michał Hałucha
Nov 22 '18 at 1:18