TypeError : takes 0 positional arguments but 1 was given [duplicate]










0















This question already has an answer here:



  • TypeError: worker() takes 0 positional arguments but 1 was given

    4 answers



I have started learning Python. I have created a class which has a function and in that function i have a dictionary . No i am checking whether a key exists in the dictionary or not , after checking i am returning a value from the function .



Now i cam trying to access the function but when when i am doing so i am getting an error "TypeError: first_func() takes 0 positional arguments but 1 was given"



Below is the code which i am using :



class myFirst:
def first_func():
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781

# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag

myclassObj = myFirst()
status = myclassObj.first_func()

if status > 1:
print ("Pass")
else:
print ("fail")









share|improve this question













marked as duplicate by Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 at 20:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    0















    This question already has an answer here:



    • TypeError: worker() takes 0 positional arguments but 1 was given

      4 answers



    I have started learning Python. I have created a class which has a function and in that function i have a dictionary . No i am checking whether a key exists in the dictionary or not , after checking i am returning a value from the function .



    Now i cam trying to access the function but when when i am doing so i am getting an error "TypeError: first_func() takes 0 positional arguments but 1 was given"



    Below is the code which i am using :



    class myFirst:
    def first_func():
    flag=0
    phonebook =
    "A" : 938477566,
    "B" : 938377264,
    "C" : 947662781

    # testing code
    if "A" in phonebook:
    flag=1
    if "D" not in phonebook:
    flag = 0
    return flag

    myclassObj = myFirst()
    status = myclassObj.first_func()

    if status > 1:
    print ("Pass")
    else:
    print ("fail")









    share|improve this question













    marked as duplicate by Jean-François Fabre python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 12 at 20:43


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      0












      0








      0








      This question already has an answer here:



      • TypeError: worker() takes 0 positional arguments but 1 was given

        4 answers



      I have started learning Python. I have created a class which has a function and in that function i have a dictionary . No i am checking whether a key exists in the dictionary or not , after checking i am returning a value from the function .



      Now i cam trying to access the function but when when i am doing so i am getting an error "TypeError: first_func() takes 0 positional arguments but 1 was given"



      Below is the code which i am using :



      class myFirst:
      def first_func():
      flag=0
      phonebook =
      "A" : 938477566,
      "B" : 938377264,
      "C" : 947662781

      # testing code
      if "A" in phonebook:
      flag=1
      if "D" not in phonebook:
      flag = 0
      return flag

      myclassObj = myFirst()
      status = myclassObj.first_func()

      if status > 1:
      print ("Pass")
      else:
      print ("fail")









      share|improve this question














      This question already has an answer here:



      • TypeError: worker() takes 0 positional arguments but 1 was given

        4 answers



      I have started learning Python. I have created a class which has a function and in that function i have a dictionary . No i am checking whether a key exists in the dictionary or not , after checking i am returning a value from the function .



      Now i cam trying to access the function but when when i am doing so i am getting an error "TypeError: first_func() takes 0 positional arguments but 1 was given"



      Below is the code which i am using :



      class myFirst:
      def first_func():
      flag=0
      phonebook =
      "A" : 938477566,
      "B" : 938377264,
      "C" : 947662781

      # testing code
      if "A" in phonebook:
      flag=1
      if "D" not in phonebook:
      flag = 0
      return flag

      myclassObj = myFirst()
      status = myclassObj.first_func()

      if status > 1:
      print ("Pass")
      else:
      print ("fail")




      This question already has an answer here:



      • TypeError: worker() takes 0 positional arguments but 1 was given

        4 answers







      python python-3.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 20:34









      Developer

      3,557174485




      3,557174485




      marked as duplicate by Jean-François Fabre python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 12 at 20:43


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Jean-François Fabre python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 12 at 20:43


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          3 Answers
          3






          active

          oldest

          votes


















          1














          methods (functions or procedures belonging to a class) require a self argument (you may be familiar with this from other languages) like so:



          class myFirst:
          def first_func(self): # here
          flag=0
          phonebook =
          "A" : 938477566,
          "B" : 938377264,
          "C" : 947662781

          # testing code
          if "A" in phonebook:
          flag=1
          if "D" not in phonebook:
          flag = 0
          return flag

          myclassObj = myFirst()
          status = myclassObj.first_func()

          if status > 1:
          print ("Pass")
          else:
          print ("fail")


          it should work with that minor change






          share|improve this answer




















          • thanks for explaining on this
            – Developer
            Nov 12 at 20:49


















          0














          The error is you're not referencing self in your function signature. Change the signature to



          def first_func(self):
          # rest of code





          share|improve this answer




























            -1














            In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status, and then you compared status to an int. Instead, you can do something like this to save a few lines-



            myClassObj = myFirst()
            if myClassObj.first_func():
            print "Pass"
            else
            print "Fail"


            The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.






            share|improve this answer



























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              methods (functions or procedures belonging to a class) require a self argument (you may be familiar with this from other languages) like so:



              class myFirst:
              def first_func(self): # here
              flag=0
              phonebook =
              "A" : 938477566,
              "B" : 938377264,
              "C" : 947662781

              # testing code
              if "A" in phonebook:
              flag=1
              if "D" not in phonebook:
              flag = 0
              return flag

              myclassObj = myFirst()
              status = myclassObj.first_func()

              if status > 1:
              print ("Pass")
              else:
              print ("fail")


              it should work with that minor change






              share|improve this answer




















              • thanks for explaining on this
                – Developer
                Nov 12 at 20:49















              1














              methods (functions or procedures belonging to a class) require a self argument (you may be familiar with this from other languages) like so:



              class myFirst:
              def first_func(self): # here
              flag=0
              phonebook =
              "A" : 938477566,
              "B" : 938377264,
              "C" : 947662781

              # testing code
              if "A" in phonebook:
              flag=1
              if "D" not in phonebook:
              flag = 0
              return flag

              myclassObj = myFirst()
              status = myclassObj.first_func()

              if status > 1:
              print ("Pass")
              else:
              print ("fail")


              it should work with that minor change






              share|improve this answer




















              • thanks for explaining on this
                – Developer
                Nov 12 at 20:49













              1












              1








              1






              methods (functions or procedures belonging to a class) require a self argument (you may be familiar with this from other languages) like so:



              class myFirst:
              def first_func(self): # here
              flag=0
              phonebook =
              "A" : 938477566,
              "B" : 938377264,
              "C" : 947662781

              # testing code
              if "A" in phonebook:
              flag=1
              if "D" not in phonebook:
              flag = 0
              return flag

              myclassObj = myFirst()
              status = myclassObj.first_func()

              if status > 1:
              print ("Pass")
              else:
              print ("fail")


              it should work with that minor change






              share|improve this answer












              methods (functions or procedures belonging to a class) require a self argument (you may be familiar with this from other languages) like so:



              class myFirst:
              def first_func(self): # here
              flag=0
              phonebook =
              "A" : 938477566,
              "B" : 938377264,
              "C" : 947662781

              # testing code
              if "A" in phonebook:
              flag=1
              if "D" not in phonebook:
              flag = 0
              return flag

              myclassObj = myFirst()
              status = myclassObj.first_func()

              if status > 1:
              print ("Pass")
              else:
              print ("fail")


              it should work with that minor change







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 at 20:36









              vencaslac

              1,002217




              1,002217











              • thanks for explaining on this
                – Developer
                Nov 12 at 20:49
















              • thanks for explaining on this
                – Developer
                Nov 12 at 20:49















              thanks for explaining on this
              – Developer
              Nov 12 at 20:49




              thanks for explaining on this
              – Developer
              Nov 12 at 20:49













              0














              The error is you're not referencing self in your function signature. Change the signature to



              def first_func(self):
              # rest of code





              share|improve this answer

























                0














                The error is you're not referencing self in your function signature. Change the signature to



                def first_func(self):
                # rest of code





                share|improve this answer























                  0












                  0








                  0






                  The error is you're not referencing self in your function signature. Change the signature to



                  def first_func(self):
                  # rest of code





                  share|improve this answer












                  The error is you're not referencing self in your function signature. Change the signature to



                  def first_func(self):
                  # rest of code






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 20:37









                  Mark Xavier

                  161




                  161





















                      -1














                      In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status, and then you compared status to an int. Instead, you can do something like this to save a few lines-



                      myClassObj = myFirst()
                      if myClassObj.first_func():
                      print "Pass"
                      else
                      print "Fail"


                      The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.






                      share|improve this answer

























                        -1














                        In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status, and then you compared status to an int. Instead, you can do something like this to save a few lines-



                        myClassObj = myFirst()
                        if myClassObj.first_func():
                        print "Pass"
                        else
                        print "Fail"


                        The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.






                        share|improve this answer























                          -1












                          -1








                          -1






                          In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status, and then you compared status to an int. Instead, you can do something like this to save a few lines-



                          myClassObj = myFirst()
                          if myClassObj.first_func():
                          print "Pass"
                          else
                          print "Fail"


                          The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.






                          share|improve this answer












                          In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status, and then you compared status to an int. Instead, you can do something like this to save a few lines-



                          myClassObj = myFirst()
                          if myClassObj.first_func():
                          print "Pass"
                          else
                          print "Fail"


                          The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 20:40









                          Hollywood

                          9211




                          9211













                              Popular posts from this blog

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              ReactJS Fetched API data displays live - need Data displayed static

                              政党