how do i make an array global

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

    how do i make an array global

    def fn():
    for i in range(l)
    global count
    count[i]= ....

    how do i declare count to be global if it is an array

    subsequently i should access or define count as an array

    error:
    global name 'count' is not defined

    thanks
    -a

  • Erik Max Francis

    #2
    Re: how do i make an array global

    a wrote:
    [color=blue]
    > def fn():
    > for i in range(l)
    > global count
    > count[i]= ....
    >
    > how do i declare count to be global if it is an array[/color]

    count = [...]

    def fn():
    global count
    for i in range(l):
    count[i] = ...

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Every human being is a problem in search of a solution.
    -- Ashley Montagu

    Comment

    • Bruno Desthuilliers

      #3
      Re: how do i make an array global

      a wrote:[color=blue]
      > def fn():
      > for i in range(l)[/color]

      l is not defined - you should have an error here.
      [color=blue]
      > global count
      > count[i]= ....
      >
      > how do i declare count to be global if it is an array[/color]

      Just like it was an integer
      [color=blue]
      > subsequently i should access or define count as an array[/color]

      You need to define count before.
      [color=blue]
      > error:
      > global name 'count' is not defined[/color]

      He...

      *but*
      You probably should not do that anyway. Globals are *evil*. And
      functions modifying globals is the worst possible thing. There are very
      few chances you *need* a global here.

      Also, and FWIW:
      - Python has lists, not arrays (there's an array type in some numerical
      package, but that's another beast)
      - 'l' is a very bad name
      - 'count' is a bad name for a list - 'counts' would be better (when I
      see the name 'count', I think of an integer)



      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • Georg Brandl

        #4
        Re: how do i make an array global

        Erik Max Francis wrote:[color=blue]
        > a wrote:
        >[color=green]
        >> def fn():
        >> for i in range(l)
        >> global count
        >> count[i]= ....
        >>
        >> how do i declare count to be global if it is an array[/color]
        >
        > count = [...]
        >
        > def fn():
        > global count
        > for i in range(l):
        > count[i] = ...
        >[/color]

        No need for "global" here.

        Georg

        Comment

        • Georg Brandl

          #5
          Re: how do i make an array global

          Bruno Desthuilliers wrote:[color=blue]
          > a wrote:[color=green]
          >> def fn():
          >> for i in range(l)[/color]
          >
          > l is not defined - you should have an error here.
          >[color=green]
          >> global count
          >> count[i]= ....
          >>
          >> how do i declare count to be global if it is an array[/color]
          >
          > Just like it was an integer[/color]

          No. If he's only mutating "count", he doesn't need a global
          declaration.
          [color=blue][color=green]
          >> subsequently i should access or define count as an array[/color]
          >
          > You need to define count before.
          >[color=green]
          >> error:
          >> global name 'count' is not defined[/color]
          >
          > He...
          >
          > *but*
          > You probably should not do that anyway. Globals are *evil*.[/color]

          Do you realize that every variable you set in a module's namespace is a
          global when used by a function? Globals are *not* evil.
          [color=blue]
          > And functions modifying globals is the worst possible thing.
          > There are very few chances you *need* a global here.[/color]

          Look at the use case first.
          For small scripts, sometimes re-assigning global names or mutating objects
          refered to by global names is essential. For large-scale packages, though,
          I agree with you that mutating globals is bad.

          Georg

          Comment

          • Bruno Desthuilliers

            #6
            Re: how do i make an array global

            Georg Brandl wrote:[color=blue]
            > Bruno Desthuilliers wrote:
            >[color=green]
            >>a wrote:
            >>[color=darkred]
            >>>def fn():
            >>> for i in range(l)[/color]
            >>
            >>l is not defined - you should have an error here.
            >>
            >>[color=darkred]
            >>> global count
            >>> count[i]= ....
            >>>
            >>>how do i declare count to be global if it is an array[/color]
            >>
            >>Just like it was an integer[/color]
            >
            >
            > No. If he's only mutating "count", he doesn't need a global
            > declaration.[/color]

            Did I said so ? I just answered the OP's question. If that's the 'int'
            that confuse you, then s/int/dict/ - what I meant is that the global
            statement doesn't care about types...
            [color=blue]
            >[color=green][color=darkred]
            >>>subsequent ly i should access or define count as an array[/color]
            >>
            >>You need to define count before.
            >>
            >>[color=darkred]
            >>>error:
            >>>global name 'count' is not defined[/color]
            >>
            >>He...
            >>
            >>*but*
            >>You probably should not do that anyway. Globals are *evil*.[/color]
            >
            > Do you realize that every variable you set in a module's namespace is a
            > global when used by a function?[/color]

            Going to teach me Python, Georg ?-) Then let's be accurate first, and
            s/variable you set/name you bind/
            [color=blue]
            > Globals are *not* evil.[/color]

            Yes they are.
            [color=blue][color=green]
            >>And functions modifying globals is the worst possible thing.
            >>There are very few chances you *need* a global here.[/color]
            >
            > Look at the use case first.[/color]

            The use case here is to avoid either passing a list as param or building
            and returning it - and the OP is obviously a newbie, so better for him
            to learn the RightThing(tm) from the beginning IMHO.
            [color=blue]
            > For small scripts, sometimes re-assigning global names or mutating objects
            > refered to by global names is essential.[/color]

            s/is essential/seems easier/

            Then you or anyone else has to make a quick fix or update, and
            everything starts to break down. Too bad.


            --
            bruno desthuilliers
            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
            p in 'onurb@xiludom. gro'.split('@')])"

            Comment

            • Georg Brandl

              #7
              Re: how do i make an array global

              Bruno Desthuilliers wrote:[color=blue]
              > Georg Brandl wrote:[color=green]
              >> Bruno Desthuilliers wrote:
              >>[color=darkred]
              >>>a wrote:
              >>>
              >>>>def fn():
              >>>> for i in range(l)
              >>>
              >>>l is not defined - you should have an error here.
              >>>
              >>>
              >>>> global count
              >>>> count[i]= ....
              >>>>
              >>>>how do i declare count to be global if it is an array
              >>>
              >>>Just like it was an integer[/color]
              >>
              >>
              >> No. If he's only mutating "count", he doesn't need a global
              >> declaration.[/color]
              >
              > Did I said so ? I just answered the OP's question. If that's the 'int'
              > that confuse you, then s/int/dict/ - what I meant is that the global
              > statement doesn't care about types...[/color]

              Ok, I misunderstood you.
              [color=blue][color=green][color=darkred]
              >>>>subsequentl y i should access or define count as an array
              >>>
              >>>You need to define count before.
              >>>
              >>>
              >>>>error:
              >>>>global name 'count' is not defined
              >>>
              >>>He...
              >>>
              >>>*but*
              >>>You probably should not do that anyway. Globals are *evil*.[/color]
              >>
              >> Do you realize that every variable you set in a module's namespace is a
              >> global when used by a function?[/color]
              >
              > Going to teach me Python, Georg ?-) Then let's be accurate first, and
              > s/variable you set/name you bind/[/color]

              Well, thanks. As if that made a difference.
              [color=blue][color=green]
              >> Globals are *not* evil.[/color]
              >
              > Yes they are.[/color]

              Really? May I tell you that in the stdlib, there are at least 13526 globals
              overall?
              [color=blue][color=green][color=darkred]
              >>>And functions modifying globals is the worst possible thing.
              >>>There are very few chances you *need* a global here.[/color]
              >>
              >> Look at the use case first.[/color]
              >
              > The use case here is to avoid either passing a list as param or building
              > and returning it - and the OP is obviously a newbie, so better for him
              > to learn the RightThing(tm) from the beginning IMHO.[/color]

              There's no reason to not use a global if it's the easiest thing to do.
              [color=blue][color=green]
              >> For small scripts, sometimes re-assigning global names or mutating objects
              >> refered to by global names is essential.[/color]
              >
              > s/is essential/seems easier/
              >
              > Then you or anyone else has to make a quick fix or update, and
              > everything starts to break down. Too bad.[/color]

              Everything starts to break down? If the change was buggy, it'll be debugged
              and corrected. That's nothing particularly related to globals. Remember,
              we're talking about one-file scripts here.

              Georg

              Comment

              • Erik Max Francis

                #8
                Re: how do i make an array global

                Georg Brandl wrote:
                [color=blue]
                > No need for "global" here.[/color]

                Yes, that's true. I was just following the original poster's lead, but
                I tend to use a `global` statement whenever I'm mutating a global in a
                local block. That works as self-documentation and means you don't have
                to be concerned about the precise case in which it's required, reducing
                bugs when you change a block so that it would have been required if you
                hadn't included it.

                --
                Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
                Seriousness is the only refuge of the shallow.
                -- Oscar Wilde

                Comment

                • a

                  #9
                  Re: how do i make an array global

                  hi bruno georg and erik
                  you are right i m a newbie,
                  i just want to do some stuff read out some stuff from a feed and
                  display it in a page, so my py code, builds this list
                  and when it goes to cheetah it gives me an error and hence i posted
                  global has fixed the error but
                  i keep getting this again and again and i dont know why? but if i
                  refresh the page error goes away and the correct content comes up
                  please help me out


                  Traceback (most recent call last):
                  File "C:\Python24\li b\site-packages\web.py ", line 2054, in
                  run_wsgi_app
                  result = self.server.app (env, self.wsgi_start _response)
                  File "C:\Python24\li b\site-packages\web.py ", line 1894, in wsgifunc
                  result = func()
                  File "C:\Python24\li b\site-packages\web.py ", line 1872, in <lambda>
                  func = lambda: handle(getattr( mod, name), mod)
                  File "C:\Python24\li b\site-packages\web.py ", line 1051, in handle
                  return tocall(*([urllib.unquote( x) for x in args] + fna))
                  File "c:\mark\web1\c ode.py", line 64, in GET
                  l_code.append( len(d_list_code[i]['entries']) )
                  IndexError: list index out of range

                  it goes off when page is refreshed

                  I m getting the following error, infrequently and if I refresh the
                  page, it just displays the page properly
                  I am unable to find the problem. How is this out of range and what
                  does
                  the error message mean?

                  Solution: Remove all .pyc files from you Zope tree and try again:
                  find <zopedir> -name "*.pyc" | xargs rm

                  How can we do this when code is running?

                  ----------------------------------------------------------------

                  Traceback (most recent call last):
                  File "C:\Python24\li b\site-packages\web.py ", line 2054, in
                  run_wsgi_app
                  result = self.server.app (env, self.wsgi_start _response)
                  File "C:\Python24\li b\site-packages\web.py ", line 1894, in wsgifunc
                  result = func()
                  File "C:\Python24\li b\site-packages\web.py ", line 1872, in <lambda>
                  func = lambda: handle(getattr( mod, name), mod)
                  File "C:\Python24\li b\site-packages\web.py ", line 1051, in handle
                  return tocall(*([urllib.unquote( x) for x in args] + fna))
                  File "c:\usr\code.py ", line 64, in GET
                  l_code.append( len(d_list_code[i]['entries']) )
                  IndexError: list index out of range
                  Erik Max Francis wrote:[color=blue]
                  > Georg Brandl wrote:
                  >[color=green]
                  > > No need for "global" here.[/color]
                  >
                  > Yes, that's true. I was just following the original poster's lead, but
                  > I tend to use a `global` statement whenever I'm mutating a global in a
                  > local block. That works as self-documentation and means you don't have
                  > to be concerned about the precise case in which it's required, reducing
                  > bugs when you change a block so that it would have been required if you
                  > hadn't included it.
                  >
                  > --
                  > Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                  > San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
                  > Seriousness is the only refuge of the shallow.
                  > -- Oscar Wilde[/color]

                  Comment

                  • a

                    #10
                    Re: how do i make an array global

                    please tell me how to do it if i should not make it global
                    like you guys worry i m not comfortable making it global and mutating
                    it in every subblock

                    thanks for reading and helping out

                    Comment

                    • Marc 'BlackJack' Rintsch

                      #11
                      Re: how do i make an array global

                      In <1151553485.378 000.203760@p79g 2000cwp.googleg roups.com>, a wrote:
                      [color=blue]
                      > Traceback (most recent call last):
                      > File "C:\Python24\li b\site-packages\web.py ", line 2054, in
                      > run_wsgi_app
                      > result = self.server.app (env, self.wsgi_start _response)
                      > File "C:\Python24\li b\site-packages\web.py ", line 1894, in wsgifunc
                      > result = func()
                      > File "C:\Python24\li b\site-packages\web.py ", line 1872, in <lambda>
                      > func = lambda: handle(getattr( mod, name), mod)
                      > File "C:\Python24\li b\site-packages\web.py ", line 1051, in handle
                      > return tocall(*([urllib.unquote( x) for x in args] + fna))
                      > File "c:\mark\web1\c ode.py", line 64, in GET
                      > l_code.append( len(d_list_code[i]['entries']) )
                      > IndexError: list index out of range
                      >
                      > it goes off when page is refreshed
                      >
                      > I m getting the following error, infrequently and if I refresh the
                      > page, it just displays the page properly
                      > I am unable to find the problem. How is this out of range and what
                      > does the error message mean?[/color]

                      The error message means that you try to access a list item that does not
                      exist::

                      In [1]: a = [1, 2, 3]

                      In [2]: a[0]
                      Out[2]: 1

                      In [3]: a[1]
                      Out[3]: 2

                      In [4]: a[2]
                      Out[4]: 3

                      In [5]: a[3]
                      -------------------------------------------------------------------------
                      exceptions.Inde xError Traceback (most recent call last)

                      /home/marc/<ipython console>

                      IndexError: list index out of range

                      So from the traceback it seems that `i` has a value that's not between 0
                      and ``len(d_list_co de)``.

                      Ciao,
                      Marc 'BlackJack' Rintsch

                      Comment

                      • a

                        #12
                        Re: how do i make an array global

                        i understand index error
                        but there is no index error in my code that is the problem!
                        Marc 'BlackJack' Rintsch wrote:[color=blue]
                        > In <1151553485.378 000.203760@p79g 2000cwp.googleg roups.com>, a wrote:
                        >[color=green]
                        > > Traceback (most recent call last):
                        > > File "C:\Python24\li b\site-packages\web.py ", line 2054, in
                        > > run_wsgi_app
                        > > result = self.server.app (env, self.wsgi_start _response)
                        > > File "C:\Python24\li b\site-packages\web.py ", line 1894, in wsgifunc
                        > > result = func()
                        > > File "C:\Python24\li b\site-packages\web.py ", line 1872, in <lambda>
                        > > func = lambda: handle(getattr( mod, name), mod)
                        > > File "C:\Python24\li b\site-packages\web.py ", line 1051, in handle
                        > > return tocall(*([urllib.unquote( x) for x in args] + fna))
                        > > File "c:\mark\web1\c ode.py", line 64, in GET
                        > > l_code.append( len(d_list_code[i]['entries']) )
                        > > IndexError: list index out of range
                        > >
                        > > it goes off when page is refreshed
                        > >
                        > > I m getting the following error, infrequently and if I refresh the
                        > > page, it just displays the page properly
                        > > I am unable to find the problem. How is this out of range and what
                        > > does the error message mean?[/color]
                        >
                        > The error message means that you try to access a list item that does not
                        > exist::
                        >
                        > In [1]: a = [1, 2, 3]
                        >
                        > In [2]: a[0]
                        > Out[2]: 1
                        >
                        > In [3]: a[1]
                        > Out[3]: 2
                        >
                        > In [4]: a[2]
                        > Out[4]: 3
                        >
                        > In [5]: a[3]
                        > -------------------------------------------------------------------------
                        > exceptions.Inde xError Traceback (most recent call last)
                        >
                        > /home/marc/<ipython console>
                        >
                        > IndexError: list index out of range
                        >
                        > So from the traceback it seems that `i` has a value that's not between 0
                        > and ``len(d_list_co de)``.
                        >
                        > Ciao,
                        > Marc 'BlackJack' Rintsch[/color]

                        Comment

                        • Fredrik Lundh

                          #13
                          Re: how do i make an array global

                          a wrote:
                          [color=blue]
                          > i understand index error
                          > but there is no index error in my code that is the problem![/color]

                          so who wrote the "c:\mark\web1\c ode.py" file ?
                          [color=blue][color=green]
                          > > File "c:\mark\web1\c ode.py", line 64, in GET
                          > > l_code.append( len(d_list_code[i]['entries']) )
                          > > IndexError: list index out of range[/color][/color]

                          maybe you should talk to Mark ?

                          </F>

                          Comment

                          Working...