UnboundLocalError

Camellia breakfastea at gmail.com
Sat Nov 11 00:35:55 EST 2006


Thank you all so much for all the replies:)

But sorry I'm so dumb I can't say I really understand,
what do I actually do when I define a function with its name "number"?
why does a name of a function has something to do with a variable?

Oh wait can I do this in Python?:
<code>
def a():
    def b()
</code>

so the b() will appear to be a local function which is the possible
cause of my little own error because the compiler will interpret the
number() as a local function but a global one?



On Nov 10, 7:32 am, Rob Williscroft <r... at freenet.co.uk> wrote:
> Terry Reedy wrote innews:mailman.1994.1163100168.11739.python-list at python.orgin
> comp.lang.python:
>
> >> def main():
> >>    number = number()
>
> > Within a function, a given name can be either global or local, but not
> > both.
> > Here you are expecting the compiler to interpret the first occurance
> > of 'number' as local and the second as global.  Humans can often
> > resolve such ambiguities correctly, but not necessarily always.  So
> > this is too much to ask of a program and hence it is not allowed.".. asked too much of the programme", sounds like a BOFH excuse to
> me ;-).
>
> Seriously I'd bet (if I were a gambling man) that this is by design,
> not either of "too much work for the interpreter" or "nobody's
> submitted a patch".
>
> IOW: Why should the intepreter do more work just so the user
> can find new and interesting ways to shoot them selves in the foot.
> 
> Rob.
> --http://www.victim-prime.dsl.pipex.com/




More information about the Python-list mailing list