Variable name has a typo, but code still works. Why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mateus

    #1

    Variable name has a typo, but code still works. Why?

    print "hello world"

    I have a nested loop where the outer loop iterates over key value pairs
    of a dictionary and the inner loop iterates over a list each list of
    which is a mapped value from the dictionary

    def showReport(self ):
    for dev, sessions in self.logger.ite ms():
    for tree in session:
    self.addTestIte m(self, tree)

    What I don't understand is why this executes w/o any problems when
    "sessions" was spelled as plural (sessionS) while later being spelled
    in the singular (session).

    Is there some type of name resolution of local variables where Python
    makes assumptions?

  • mateus

    #2
    Re: Variable name has a typo, but code still works. Why?


    mateus wrote:[color=blue]
    > print "hello world"
    >
    > I have a nested loop where the outer loop iterates over key value pairs
    > of a dictionary and the inner loop iterates over a list each list of
    > which is a mapped value from the dictionary
    >
    > def showReport(self ):
    > for dev, sessions in self.logger.ite ms():
    > for tree in session:
    > self.addTestIte m(self, tree)
    >
    > What I don't understand is why this executes w/o any problems when
    > "sessions" was spelled as plural (sessionS) while later being spelled
    > in the singular (session).
    >
    > Is there some type of name resolution of local variables where Python
    > makes assumptions?[/color]

    A few specs that I forgot to mention.

    Version: python-2.4.2
    OS: Librix (Gentoo (Linux))

    Comment

    • hwiechers@gmail.com

      #3
      Re: Variable name has a typo, but code still works. Why?


      mateus wrote:[color=blue]
      > print "hello world"
      >
      > I have a nested loop where the outer loop iterates over key value pairs
      > of a dictionary and the inner loop iterates over a list each list of
      > which is a mapped value from the dictionary
      >
      > def showReport(self ):
      > for dev, sessions in self.logger.ite ms():
      > for tree in session:
      > self.addTestIte m(self, tree)
      >
      > What I don't understand is why this executes w/o any problems when
      > "sessions" was spelled as plural (sessionS) while later being spelled
      > in the singular (session).
      >
      > Is there some type of name resolution of local variables where Python
      > makes assumptions?[/color]

      I've never heard of a rule disregarding ending 's'es and I really doubt
      one
      exists.

      Are you sure session isn't a global variable? You can check using
      globals().

      Comment

      • Tim N. van der Leeuw

        #4
        Re: Variable name has a typo, but code still works. Why?

        Hi Mateus,

        We'd need to see more code then just this snippet. It looks like the
        name 'session' is used elsewhere in the code, and is in scope for the
        showReport() method.
        But without seeing a bit more code of this class, and possibly global
        variables / code, it's not possible to say this.

        There's definately no rules in Python that allow you to 'abbreviate'
        variable names.

        Cheers,

        --Tim

        mateus wrote:[color=blue]
        > print "hello world"
        >
        > I have a nested loop where the outer loop iterates over key value pairs
        > of a dictionary and the inner loop iterates over a list each list of
        > which is a mapped value from the dictionary
        >
        > def showReport(self ):
        > for dev, sessions in self.logger.ite ms():
        > for tree in session:
        > self.addTestIte m(self, tree)
        >
        > What I don't understand is why this executes w/o any problems when
        > "sessions" was spelled as plural (sessionS) while later being spelled
        > in the singular (session).
        >
        > Is there some type of name resolution of local variables where Python
        > makes assumptions?[/color]

        Comment

        • Max M

          #5
          Re: Variable name has a typo, but code still works. Why?

          mateus wrote:[color=blue]
          > print "hello world"
          >
          > I have a nested loop where the outer loop iterates over key value pairs
          > of a dictionary and the inner loop iterates over a list each list of
          > which is a mapped value from the dictionary
          >
          > def showReport(self ):
          > for dev, sessions in self.logger.ite ms():
          > for tree in session:
          > self.addTestIte m(self, tree)
          >
          > What I don't understand is why this executes w/o any problems when
          > "sessions" was spelled as plural (sessionS) while later being spelled
          > in the singular (session).
          >
          > Is there some type of name resolution of local variables where Python
          > makes assumptions?[/color]


          No. You are probably running your script in an ide that keeps an old
          variable hanging around.

          Try it from a command promt.


          --

          hilsen/regards Max M, Denmark

          A small collection of CLAP synths and effects inspired by classic hardware.

          IT's Mad Science

          Phone: +45 66 11 84 94
          Mobile: +45 29 93 42 96

          Comment

          • Max Erickson

            #6
            Re: Variable name has a typo, but code still works. Why?

            "mateus" <mateus.justino @gmail.com> wrote:
            [color=blue]
            > print "hello world"
            >
            > I have a nested loop where the outer loop iterates over key value
            > pairs of a dictionary and the inner loop iterates over a list
            > each list of which is a mapped value from the dictionary
            >
            > def showReport(self ):
            > for dev, sessions in self.logger.ite ms():
            > for tree in session:
            > self.addTestIte m(self, tree)
            >
            > What I don't understand is why this executes w/o any problems
            > when "sessions" was spelled as plural (sessionS) while later
            > being spelled in the singular (session).
            >
            > Is there some type of name resolution of local variables where
            > Python makes assumptions?
            >[/color]

            Is self.logger.ite ms() empty?

            This does not raise a NameError even when 'explode' is not defined:

            for x in []:
            explode


            max

            Comment

            • mateus

              #7
              Re: Variable name has a typo, but code still works. Why?

              Well, one by one I checked for the presence of both sessions and
              session in the globals dictionary (within showReport(), but outside of
              the loops).

              Neither one of them existed previously, thus and I received the
              exception about them not being found:


              File "/home/mjpl/hct/repository/hct/tutoo.py", line 219, in loadNext
              self.loadStage( self.cur+1)
              File "/home/mjpl/hct/repository/hct/tutoo.py", line 195, in loadStage
              self.stageFrame .show()
              File
              "/home/mjpl/hct/repository/hct/stages/resultadoframe/resultadoframe. py",
              line 17, in show
              self.listView.s howReport()
              File "/home/mjpl/hct/repository/hct/widgets/deviceview.py", line 54,
              in showReport
              print globals()['session']
              KeyError: 'session'

              So, I tried using other variable names for the outer and inner loops
              with the only difference of one letter. I then got the expected
              message about the variable name not being encountered.

              I returned the variable names to 'sessions' and 'session' respectively,
              and I got the same error about the name 'session' not being founded. I
              can only assume that there was some type of cache problem. Could it
              have been in the .pyc? I thought that was recompiled every time a .py
              is run/set to be interpreted. I'm sure I got that last sentence wrong.

              hwiechers@gmail .com wrote:[color=blue]
              > mateus wrote:[color=green]
              > > print "hello world"
              > >
              > > I have a nested loop where the outer loop iterates over key value pairs
              > > of a dictionary and the inner loop iterates over a list each list of
              > > which is a mapped value from the dictionary
              > >
              > > def showReport(self ):
              > > for dev, sessions in self.logger.ite ms():
              > > for tree in session:
              > > self.addTestIte m(self, tree)
              > >
              > > What I don't understand is why this executes w/o any problems when
              > > "sessions" was spelled as plural (sessionS) while later being spelled
              > > in the singular (session).
              > >
              > > Is there some type of name resolution of local variables where Python
              > > makes assumptions?[/color]
              >
              > I've never heard of a rule disregarding ending 's'es and I really doubt
              > one
              > exists.
              >
              > Are you sure session isn't a global variable? You can check using
              > globals().[/color]

              Comment

              • BartlebyScrivener

                #8
                Re: Variable name has a typo, but code still works. Why?

                > I can only assume that there was some type of cache problem. Could it[color=blue]
                > have been in the .pyc? I thought that was recompiled every time a .py
                > is run/set to be interpreted.[/color]

                If you are on Windows, check your PATHEXT environment variable and make
                sure that .py is listed ahead of .pyc and any other .py?

                See, e.g., a bug in earlier versions of ActiveState distrib

                http://tinyurl.com/qok9v

                rd

                "Electricit y is actually made up of extremely tiny particles called
                electrons, that you cannot see with the naked eye unless you have been
                drinking."--Dave Barry

                Comment

                Working...