Catch exception “missing 1 required positional argument” if I don't send two arguments?












-1















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"









share|improve this question

























  • 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" 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
















-1















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"









share|improve this question

























  • 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" 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














-1












-1








-1








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"









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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











  • 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






  • 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" 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

















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












1 Answer
1






active

oldest

votes


















0














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"





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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"





    share|improve this answer




























      0














      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"





      share|improve this answer


























        0












        0








        0







        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"





        share|improve this answer













        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"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 11:21









        Michał HałuchaMichał Hałucha

        12




        12






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin