Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2012-04-24 05:52:28
Size: 1028
Editor: 58-7-169-58
Comment:
Revision 4 as of 2012-04-24 20:58:40
Size: 2432
Editor: PaulBoddie
Comment: Used a more descriptive name.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from Asking for Help/globals
Line 4: Line 5:
= Asking for Help: ... = = Asking for Help: Why is my global not assigned? =
Line 10: Line 11:
## {{{#!python
## def fun():
{{{#!python
def fun():
Line 28: Line 29:
## }}}

## Leave the note below so that editors can follow the instructions...

{{{#!wiki note
When ''answering'' questions, add the CategoryAskingForHelpAnswered category when saving the page. This will move the link to this page from the questions section to the answers section on the [[Asking for Help]] page.
Line 36: Line 31:
The purpose of `global` is to indicate which names are global in that specific part of a program. So, although you have indicated that `b` is global in `fun`, you still need to indicate that `b` is global in `fun1`. Otherwise, Python doesn't know where to get `b` from. It's actually slightly more complicated than this; for more, see below.

Now, you do manage to get away with not declaring `a` as global in both functions. The reason for this is that you aren't changing what `a` refers to, but are instead modifying an item (or element) in what `a` refers to (a list). So, Python deduces that `a` is a global and doesn't change its mind about it: `a` isn't itself made to refer to something else in `fun1`, so any reference to `a` is always to a global object.

So, back to the problem with `b`. When Python compiles `fun1`, it has to consider that `b` itself is being assigned to and that there's no `global` declaration for `b` in `fun1`. So, Python then believes `b` to be a local. But then, in the assignment itself, it has to get the value of `b` (think of it as `b + 1; b = <result>`), and at that point there is no such local, so you get the error.

In short, Python has to decide whether a name is local or global and stick with that decision for every mention of that name in a function: it can't switch between the two since it has to generate code that builds on the assumption that a name is always local or always global in that function. So you should always declare names as global in any function where you are assigning to them as globals. -- PaulBoddie <<DateTime(2012-04-24T21:57:57+0100)>>
Line 37: Line 40:
CategoryAskingForHelp CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help: Why is my global not assigned?

The following puzzles me: Both a and b are globals. Both are assigned an initial value in fun(). But b is considered unassigned in fun1().

   1 def fun():
   2     global a,b
   3     a = [0]*1
   4     b = 0
   5     fun1()
   6 
   7 def fun1():
   8     a[0] += 1
   9     print('a[0]: ', a[0])
  10     b += 1
  11     print('b: ', b)
  12 
  13 
  14 fun()
  15 
  16  b += 1
  17 UnboundLocalError: local variable 'b' referenced before assignment

The purpose of global is to indicate which names are global in that specific part of a program. So, although you have indicated that b is global in fun, you still need to indicate that b is global in fun1. Otherwise, Python doesn't know where to get b from. It's actually slightly more complicated than this; for more, see below.

Now, you do manage to get away with not declaring a as global in both functions. The reason for this is that you aren't changing what a refers to, but are instead modifying an item (or element) in what a refers to (a list). So, Python deduces that a is a global and doesn't change its mind about it: a isn't itself made to refer to something else in fun1, so any reference to a is always to a global object.

So, back to the problem with b. When Python compiles fun1, it has to consider that b itself is being assigned to and that there's no global declaration for b in fun1. So, Python then believes b to be a local. But then, in the assignment itself, it has to get the value of b (think of it as b + 1; b = <result>), and at that point there is no such local, so you get the error.

In short, Python has to decide whether a name is local or global and stick with that decision for every mention of that name in a function: it can't switch between the two since it has to generate code that builds on the assumption that a name is always local or always global in that function. So you should always declare names as global in any function where you are assigning to them as globals. -- PaulBoddie 2012-04-24 20:57:57


CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help/Why is my global not assigned? (last edited 2012-04-24 20:58:40 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.