NameError: name is not defined in python init function










1















class HumidityServer(CoAP):
def __init__(self, host, port, noOfSensors=10, multicast=False):
CoAP.__init__(self, (host, port), multicast)

for num in range(noOfSensors):
self.add_resource('humidity'+num+'/', HumidityResource(num))


This excerpt is part of a program that generates:



Traceback (most recent call last):
File "humidityserver.py", line 10, in <module>
class HumidityServer(CoAP):
File "humidityserver.py", line 14, in HumidityServer
for num in range(noOfSensors):
NameError: name 'noOfSensors' is not defined


Why does this happen even though I've defined a default value for the variable?










share|improve this question


























    1















    class HumidityServer(CoAP):
    def __init__(self, host, port, noOfSensors=10, multicast=False):
    CoAP.__init__(self, (host, port), multicast)

    for num in range(noOfSensors):
    self.add_resource('humidity'+num+'/', HumidityResource(num))


    This excerpt is part of a program that generates:



    Traceback (most recent call last):
    File "humidityserver.py", line 10, in <module>
    class HumidityServer(CoAP):
    File "humidityserver.py", line 14, in HumidityServer
    for num in range(noOfSensors):
    NameError: name 'noOfSensors' is not defined


    Why does this happen even though I've defined a default value for the variable?










    share|improve this question
























      1












      1








      1








      class HumidityServer(CoAP):
      def __init__(self, host, port, noOfSensors=10, multicast=False):
      CoAP.__init__(self, (host, port), multicast)

      for num in range(noOfSensors):
      self.add_resource('humidity'+num+'/', HumidityResource(num))


      This excerpt is part of a program that generates:



      Traceback (most recent call last):
      File "humidityserver.py", line 10, in <module>
      class HumidityServer(CoAP):
      File "humidityserver.py", line 14, in HumidityServer
      for num in range(noOfSensors):
      NameError: name 'noOfSensors' is not defined


      Why does this happen even though I've defined a default value for the variable?










      share|improve this question














      class HumidityServer(CoAP):
      def __init__(self, host, port, noOfSensors=10, multicast=False):
      CoAP.__init__(self, (host, port), multicast)

      for num in range(noOfSensors):
      self.add_resource('humidity'+num+'/', HumidityResource(num))


      This excerpt is part of a program that generates:



      Traceback (most recent call last):
      File "humidityserver.py", line 10, in <module>
      class HumidityServer(CoAP):
      File "humidityserver.py", line 14, in HumidityServer
      for num in range(noOfSensors):
      NameError: name 'noOfSensors' is not defined


      Why does this happen even though I've defined a default value for the variable?







      python init nameerror






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 28 '16 at 14:14









      SahandSahand

      1,65812444




      1,65812444






















          3 Answers
          3






          active

          oldest

          votes


















          1














          You are mixing tabs and spaces in your code; this is your original code as pasted into the question:



          enter image description here



          The solid grey lines are tabs, the dots are spaces.



          Note how the for loop is indented to 8 spaces, buth def __init__ is indented by one tab? Python expands tabs to eight spaces, not four, so to Python your code looks like this instead:



          source code at 8 spaces per tab



          Now you can see that the for loop is outside the __init__ method, and the noOfSensors variable from the __init__ function signature is not defined there.



          Don't mix tabs and spaces in indentation, stick to just tabs or just spaces. The PEP 8 Python style guide strongly advises you to use only spaces for indentation. Your editor can easily be configured to insert spaces whenever you use the TAB key, for example.






          share|improve this answer

























          • Great explanation, thx

            – Sahand
            Apr 28 '16 at 14:23











          • What text editor are your excerpts from?

            – Sahand
            Apr 28 '16 at 14:25











          • @Sandi: Sublime Text 3, using the Monokay colour scheme.

            – Martijn Pieters
            Apr 28 '16 at 14:25



















          0














          I copied and run the code, it was not because of the mixing tabs and spaces issues answered by @Martijn. I have just ran into a similar issue while creating a small game based on classes.



          Even though I have assigned a default value into the variable but it stuck and gave me error:



          NameError: name 'mental' is not defined #where mental is the variable


          I research a bit and saw somebody talking about instances. Then I tried to create an instance and make the instance execute the function, meanwhile I define a function to what I intended to execute. And it works out. Below is my example on the fix:



          class People(object):
          def __init__(self, vital, mental):
          self.vital = vital
          self.mental = mental

          class Andy(People):
          print "My name is Andy... I am not the killer! Trust me..."
          chat1 = raw_input(">")
          if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
          self.mental += -1
          print "(checking) option 1"
          elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
          self.mental += 1
          print "(checking) option 2"
          else:
          print "Pass to else"
          print self.mental
          print self.vital

          andy = Andy(1, 5)


          The solution I found was:



          class People(object):
          def __init__(self, vital, mental):
          self.vital = vital
          self.mental = mental

          class Andy(People):
          def play(self):
          print "My name is Andy... I am not the killer! Trust me..."
          chat1 = raw_input(">")
          if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
          self.mental += -1
          print "(checking) option 1"
          elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
          self.mental += 1
          print "(checking) option 2"
          else:
          print "Pass to else"
          print self.mental
          print self.vital

          andy = Andy(1, 5)
          andy.play()


          Maybe there are other solutions to your question, but I am quite new in programming, there are stuff in your code I don't understand. But regarding to the error you get I think its because of the 'self' has to be an instance you set for it to run through the class. Please correct me if I got the concept wrong.






          share|improve this answer






























            -3














            it's not instanced Yet so the defalut value executed when class is Created man






            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%2f36917318%2fnameerror-name-is-not-defined-in-python-init-function%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You are mixing tabs and spaces in your code; this is your original code as pasted into the question:



              enter image description here



              The solid grey lines are tabs, the dots are spaces.



              Note how the for loop is indented to 8 spaces, buth def __init__ is indented by one tab? Python expands tabs to eight spaces, not four, so to Python your code looks like this instead:



              source code at 8 spaces per tab



              Now you can see that the for loop is outside the __init__ method, and the noOfSensors variable from the __init__ function signature is not defined there.



              Don't mix tabs and spaces in indentation, stick to just tabs or just spaces. The PEP 8 Python style guide strongly advises you to use only spaces for indentation. Your editor can easily be configured to insert spaces whenever you use the TAB key, for example.






              share|improve this answer

























              • Great explanation, thx

                – Sahand
                Apr 28 '16 at 14:23











              • What text editor are your excerpts from?

                – Sahand
                Apr 28 '16 at 14:25











              • @Sandi: Sublime Text 3, using the Monokay colour scheme.

                – Martijn Pieters
                Apr 28 '16 at 14:25
















              1














              You are mixing tabs and spaces in your code; this is your original code as pasted into the question:



              enter image description here



              The solid grey lines are tabs, the dots are spaces.



              Note how the for loop is indented to 8 spaces, buth def __init__ is indented by one tab? Python expands tabs to eight spaces, not four, so to Python your code looks like this instead:



              source code at 8 spaces per tab



              Now you can see that the for loop is outside the __init__ method, and the noOfSensors variable from the __init__ function signature is not defined there.



              Don't mix tabs and spaces in indentation, stick to just tabs or just spaces. The PEP 8 Python style guide strongly advises you to use only spaces for indentation. Your editor can easily be configured to insert spaces whenever you use the TAB key, for example.






              share|improve this answer

























              • Great explanation, thx

                – Sahand
                Apr 28 '16 at 14:23











              • What text editor are your excerpts from?

                – Sahand
                Apr 28 '16 at 14:25











              • @Sandi: Sublime Text 3, using the Monokay colour scheme.

                – Martijn Pieters
                Apr 28 '16 at 14:25














              1












              1








              1







              You are mixing tabs and spaces in your code; this is your original code as pasted into the question:



              enter image description here



              The solid grey lines are tabs, the dots are spaces.



              Note how the for loop is indented to 8 spaces, buth def __init__ is indented by one tab? Python expands tabs to eight spaces, not four, so to Python your code looks like this instead:



              source code at 8 spaces per tab



              Now you can see that the for loop is outside the __init__ method, and the noOfSensors variable from the __init__ function signature is not defined there.



              Don't mix tabs and spaces in indentation, stick to just tabs or just spaces. The PEP 8 Python style guide strongly advises you to use only spaces for indentation. Your editor can easily be configured to insert spaces whenever you use the TAB key, for example.






              share|improve this answer















              You are mixing tabs and spaces in your code; this is your original code as pasted into the question:



              enter image description here



              The solid grey lines are tabs, the dots are spaces.



              Note how the for loop is indented to 8 spaces, buth def __init__ is indented by one tab? Python expands tabs to eight spaces, not four, so to Python your code looks like this instead:



              source code at 8 spaces per tab



              Now you can see that the for loop is outside the __init__ method, and the noOfSensors variable from the __init__ function signature is not defined there.



              Don't mix tabs and spaces in indentation, stick to just tabs or just spaces. The PEP 8 Python style guide strongly advises you to use only spaces for indentation. Your editor can easily be configured to insert spaces whenever you use the TAB key, for example.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 28 '16 at 14:23

























              answered Apr 28 '16 at 14:21









              Martijn PietersMartijn Pieters

              710k13724782299




              710k13724782299












              • Great explanation, thx

                – Sahand
                Apr 28 '16 at 14:23











              • What text editor are your excerpts from?

                – Sahand
                Apr 28 '16 at 14:25











              • @Sandi: Sublime Text 3, using the Monokay colour scheme.

                – Martijn Pieters
                Apr 28 '16 at 14:25


















              • Great explanation, thx

                – Sahand
                Apr 28 '16 at 14:23











              • What text editor are your excerpts from?

                – Sahand
                Apr 28 '16 at 14:25











              • @Sandi: Sublime Text 3, using the Monokay colour scheme.

                – Martijn Pieters
                Apr 28 '16 at 14:25

















              Great explanation, thx

              – Sahand
              Apr 28 '16 at 14:23





              Great explanation, thx

              – Sahand
              Apr 28 '16 at 14:23













              What text editor are your excerpts from?

              – Sahand
              Apr 28 '16 at 14:25





              What text editor are your excerpts from?

              – Sahand
              Apr 28 '16 at 14:25













              @Sandi: Sublime Text 3, using the Monokay colour scheme.

              – Martijn Pieters
              Apr 28 '16 at 14:25






              @Sandi: Sublime Text 3, using the Monokay colour scheme.

              – Martijn Pieters
              Apr 28 '16 at 14:25














              0














              I copied and run the code, it was not because of the mixing tabs and spaces issues answered by @Martijn. I have just ran into a similar issue while creating a small game based on classes.



              Even though I have assigned a default value into the variable but it stuck and gave me error:



              NameError: name 'mental' is not defined #where mental is the variable


              I research a bit and saw somebody talking about instances. Then I tried to create an instance and make the instance execute the function, meanwhile I define a function to what I intended to execute. And it works out. Below is my example on the fix:



              class People(object):
              def __init__(self, vital, mental):
              self.vital = vital
              self.mental = mental

              class Andy(People):
              print "My name is Andy... I am not the killer! Trust me..."
              chat1 = raw_input(">")
              if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
              self.mental += -1
              print "(checking) option 1"
              elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
              self.mental += 1
              print "(checking) option 2"
              else:
              print "Pass to else"
              print self.mental
              print self.vital

              andy = Andy(1, 5)


              The solution I found was:



              class People(object):
              def __init__(self, vital, mental):
              self.vital = vital
              self.mental = mental

              class Andy(People):
              def play(self):
              print "My name is Andy... I am not the killer! Trust me..."
              chat1 = raw_input(">")
              if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
              self.mental += -1
              print "(checking) option 1"
              elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
              self.mental += 1
              print "(checking) option 2"
              else:
              print "Pass to else"
              print self.mental
              print self.vital

              andy = Andy(1, 5)
              andy.play()


              Maybe there are other solutions to your question, but I am quite new in programming, there are stuff in your code I don't understand. But regarding to the error you get I think its because of the 'self' has to be an instance you set for it to run through the class. Please correct me if I got the concept wrong.






              share|improve this answer



























                0














                I copied and run the code, it was not because of the mixing tabs and spaces issues answered by @Martijn. I have just ran into a similar issue while creating a small game based on classes.



                Even though I have assigned a default value into the variable but it stuck and gave me error:



                NameError: name 'mental' is not defined #where mental is the variable


                I research a bit and saw somebody talking about instances. Then I tried to create an instance and make the instance execute the function, meanwhile I define a function to what I intended to execute. And it works out. Below is my example on the fix:



                class People(object):
                def __init__(self, vital, mental):
                self.vital = vital
                self.mental = mental

                class Andy(People):
                print "My name is Andy... I am not the killer! Trust me..."
                chat1 = raw_input(">")
                if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
                self.mental += -1
                print "(checking) option 1"
                elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
                self.mental += 1
                print "(checking) option 2"
                else:
                print "Pass to else"
                print self.mental
                print self.vital

                andy = Andy(1, 5)


                The solution I found was:



                class People(object):
                def __init__(self, vital, mental):
                self.vital = vital
                self.mental = mental

                class Andy(People):
                def play(self):
                print "My name is Andy... I am not the killer! Trust me..."
                chat1 = raw_input(">")
                if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
                self.mental += -1
                print "(checking) option 1"
                elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
                self.mental += 1
                print "(checking) option 2"
                else:
                print "Pass to else"
                print self.mental
                print self.vital

                andy = Andy(1, 5)
                andy.play()


                Maybe there are other solutions to your question, but I am quite new in programming, there are stuff in your code I don't understand. But regarding to the error you get I think its because of the 'self' has to be an instance you set for it to run through the class. Please correct me if I got the concept wrong.






                share|improve this answer

























                  0












                  0








                  0







                  I copied and run the code, it was not because of the mixing tabs and spaces issues answered by @Martijn. I have just ran into a similar issue while creating a small game based on classes.



                  Even though I have assigned a default value into the variable but it stuck and gave me error:



                  NameError: name 'mental' is not defined #where mental is the variable


                  I research a bit and saw somebody talking about instances. Then I tried to create an instance and make the instance execute the function, meanwhile I define a function to what I intended to execute. And it works out. Below is my example on the fix:



                  class People(object):
                  def __init__(self, vital, mental):
                  self.vital = vital
                  self.mental = mental

                  class Andy(People):
                  print "My name is Andy... I am not the killer! Trust me..."
                  chat1 = raw_input(">")
                  if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
                  self.mental += -1
                  print "(checking) option 1"
                  elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
                  self.mental += 1
                  print "(checking) option 2"
                  else:
                  print "Pass to else"
                  print self.mental
                  print self.vital

                  andy = Andy(1, 5)


                  The solution I found was:



                  class People(object):
                  def __init__(self, vital, mental):
                  self.vital = vital
                  self.mental = mental

                  class Andy(People):
                  def play(self):
                  print "My name is Andy... I am not the killer! Trust me..."
                  chat1 = raw_input(">")
                  if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
                  self.mental += -1
                  print "(checking) option 1"
                  elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
                  self.mental += 1
                  print "(checking) option 2"
                  else:
                  print "Pass to else"
                  print self.mental
                  print self.vital

                  andy = Andy(1, 5)
                  andy.play()


                  Maybe there are other solutions to your question, but I am quite new in programming, there are stuff in your code I don't understand. But regarding to the error you get I think its because of the 'self' has to be an instance you set for it to run through the class. Please correct me if I got the concept wrong.






                  share|improve this answer













                  I copied and run the code, it was not because of the mixing tabs and spaces issues answered by @Martijn. I have just ran into a similar issue while creating a small game based on classes.



                  Even though I have assigned a default value into the variable but it stuck and gave me error:



                  NameError: name 'mental' is not defined #where mental is the variable


                  I research a bit and saw somebody talking about instances. Then I tried to create an instance and make the instance execute the function, meanwhile I define a function to what I intended to execute. And it works out. Below is my example on the fix:



                  class People(object):
                  def __init__(self, vital, mental):
                  self.vital = vital
                  self.mental = mental

                  class Andy(People):
                  print "My name is Andy... I am not the killer! Trust me..."
                  chat1 = raw_input(">")
                  if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
                  self.mental += -1
                  print "(checking) option 1"
                  elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
                  self.mental += 1
                  print "(checking) option 2"
                  else:
                  print "Pass to else"
                  print self.mental
                  print self.vital

                  andy = Andy(1, 5)


                  The solution I found was:



                  class People(object):
                  def __init__(self, vital, mental):
                  self.vital = vital
                  self.mental = mental

                  class Andy(People):
                  def play(self):
                  print "My name is Andy... I am not the killer! Trust me..."
                  chat1 = raw_input(">")
                  if chat1 == ('i believe you' or 'yes i believe' or 'believe' or 'i trust you' or 'yes i trust you'):
                  self.mental += -1
                  print "(checking) option 1"
                  elif chat1 == ('you are killer' or 'you are the one' or 'really?' or 'i doubt' or 'i don't believe' or 'i don't trust you'):
                  self.mental += 1
                  print "(checking) option 2"
                  else:
                  print "Pass to else"
                  print self.mental
                  print self.vital

                  andy = Andy(1, 5)
                  andy.play()


                  Maybe there are other solutions to your question, but I am quite new in programming, there are stuff in your code I don't understand. But regarding to the error you get I think its because of the 'self' has to be an instance you set for it to run through the class. Please correct me if I got the concept wrong.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 3:53









                  Will MeetYouWill MeetYou

                  237




                  237





















                      -3














                      it's not instanced Yet so the defalut value executed when class is Created man






                      share|improve this answer



























                        -3














                        it's not instanced Yet so the defalut value executed when class is Created man






                        share|improve this answer

























                          -3












                          -3








                          -3







                          it's not instanced Yet so the defalut value executed when class is Created man






                          share|improve this answer













                          it's not instanced Yet so the defalut value executed when class is Created man







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 28 '16 at 14:18









                          Cod3rCod3r

                          64




                          64



























                              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%2f36917318%2fnameerror-name-is-not-defined-in-python-init-function%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







                              這個網誌中的熱門文章

                              Barbados

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3