By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

UnboundLocalError

P: n/a
hi all

why it generates an "UnboundLocalError" when I do the following:

<code>
....
def main():
number = number()
number_user = user_guess()
while number_user != number:
check_number(number = number, number_user = number_user)
number_user = user_guess()

UnboundLocalError: local variable 'number' referenced before assignment
</code>

I found when I changed the number() to num() or whatever the issue
solved
but doesn't every function has its own namespace?
Can anyone please explain it to me?

Peace

Nov 9 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hello,

Camellia wrote:
why it generates an "UnboundLocalError" when I do the following:

<code>
...
def main():
number = number()
number_user = user_guess()
while number_user != number:
check_number(number = number, number_user = number_user)
number_user = user_guess()

UnboundLocalError: local variable 'number' referenced before assignment
</code>

I found when I changed the number() to num() or whatever the issue
solved
but doesn't every function has its own namespace?
Can anyone please explain it to me?
When Python compiles your code, it sees that you are using a local
variable 'number' in this function ('number = ...') and probably allocates
some memory for it or whatever - ask the core Python hackers for details.
When this statement is executed, the right hand side is evaluated first and
Python finds a reference to 'number'. It knows that number is a local
variable in main(), but has not been assigned yet (which would happend
after the right hand side is evaluated) - thus the error.

Even if you could get this to work, you have two 'number's in the function
with different meaning, which just makes understanding the code too
difficult.

Is number() (the global one) a function or class. I guess it's a function,
so I would suggest to name it 'get_number' (and same
for 'get_user_guess') - this makes the code more descriptive. Should it
really be a class, a common convention is to capitalize it 'Number()'.
HTH

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
Nov 9 '06 #2

P: n/a

"Camellia" <br*********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
hi all

why it generates an "UnboundLocalError" when I do the following:

<code>
...
Presumably, the elided code includes a 'def number():' statement
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.

As Benjamin said, use two different names for the two different objects
that you need to use in the same context.

Terry Jan Reedy


Nov 9 '06 #3

P: n/a
Terry Reedy wrote in
news:ma***************************************@pyt hon.org in
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/
Nov 9 '06 #4

P: n/a
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...@freenet.co.ukwrote:
Terry Reedy wrote innews:ma***************************************@p ython.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/
Nov 11 '06 #5

P: n/a
At Saturday 11/11/2006 02:35, Camellia wrote:
>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"?
Don't apologize, Python is a lot more dumb than you. It obeys very
simple rules (a good thing, so we can clearly understand them, once
we know them).
Recalling your previous example:
>def main():
> number = number()
Python first scans the source code looking for assigned-to names.
That is, names to the left of equal signs. Those names, plus the
function formal parameters, make the list of "local names". Any other
names referenced are assumed to be globals, that is, living outside
your function. (That's not entirely true but enough for now).
Notice that "number" is a local name because it's assigned to; it
doesn't matter whether a global name "number" exists or not.
When the code is executed, the "number" to the right references the
local name, which has not been assigned to yet - that's why you get
an UnboundLocalError. "number" can't be a global name when used on
the right hand side, and a local name when used on the left hand
side. It's simple: it is local, or not, but not both.
>why does a name of a function has something to do with a variable?
Notice also that it does not matter *what* kind of object a name
refers to: it may be a function, a class, an object instance,
whatever. Inside a function, by example, a local name "a" may be
bound at most to a single object at a time, it doesn't matter its
type. A local name "a" can't refer to an integer and a class at the same time.
The left hand side of an assign statement *always* uses local names,
except when you declare a name to be global by using the "global" keyword.
And notice also that I've never used the word 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?
Yes, you can. Python has "lexically nested scopes". The simple
local/global rule of above is a bit more complicated: when a name is
not local, it is searched inside the enclosing functions, then in the
containing module's global namespace, and last in the builtin names.
And yes, b is local to a, so it effectively hides any external b that
could be in outer scopes.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 11 '06 #6

P: n/a
Oh how can I thank you enough, you make my day:)
According to what you said I finally figure it out, it is the same as:

<code>
b = 1
def a():
b = b #no good:)
</code>

So in every day programming I should avoid using the same name for
different objects because they will step on each other, right?

On Nov 11, 6:18 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
At Saturday 11/11/2006 02:35, Camellia wrote:
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"?Don't apologize, Python is a lot more dumb than you. It obeys very
simple rules (a good thing, so we can clearly understand them, once
we know them).
Recalling your previous example:
def main():
number = number()Python first scans the source code looking for assigned-to names.
That is, names to the left of equal signs. Those names, plus the
function formal parameters, make the list of "local names". Any other
names referenced are assumed to be globals, that is, living outside
your function. (That's not entirely true but enough for now).
Notice that "number" is a local name because it's assigned to; it
doesn't matter whether a global name "number" exists or not.
When the code is executed, the "number" to the right references the
local name, which has not been assigned to yet - that's why you get
an UnboundLocalError. "number" can't be a global name when used on
the right hand side, and a local name when used on the left hand
side. It's simple: it is local, or not, but not both.
why does a name of a function has something to do with a variable?Noticealso that it does not matter *what* kind of object a name
refers to: it may be a function, a class, an object instance,
whatever. Inside a function, by example, a local name "a" may be
bound at most to a single object at a time, it doesn't matter its
type. A local name "a" can't refer to an integer and a class at the same time.
The left hand side of an assign statement *always* uses local names,
except when you declare a name to be global by using the "global" keyword.
And notice also that I've never used the word 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?Yes, you can. Python has "lexically nested scopes". The simple
local/global rule of above is a bit more complicated: when a name is
not local, it is searched inside the enclosing functions, then in the
containing module's global namespace, and last in the builtin names.
And yes, b is local to a, so it effectively hides any external b that
could be in outer scopes.

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! -http://correo.yahoo.com.ar
Nov 11 '06 #7

P: n/a
Oh how can I thank you enough, you make my day:)
According to what you said I finally figure it out, it is the same as:

<code>
b = 1
def a():
b = b #no good:)
</code>

So in every day programming I should avoid using the same name for
different types of objects because they will step on each other, right?

On Nov 11, 6:18 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:

On Nov 11, 6:18 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
At Saturday 11/11/2006 02:35, Camellia wrote:
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"?Don't apologize, Python is a lot more dumb than you. It obeys very
simple rules (a good thing, so we can clearly understand them, once
we know them).
Recalling your previous example:
def main():
number = number()Python first scans the source code looking for assigned-to names.
That is, names to the left of equal signs. Those names, plus the
function formal parameters, make the list of "local names". Any other
names referenced are assumed to be globals, that is, living outside
your function. (That's not entirely true but enough for now).
Notice that "number" is a local name because it's assigned to; it
doesn't matter whether a global name "number" exists or not.
When the code is executed, the "number" to the right references the
local name, which has not been assigned to yet - that's why you get
an UnboundLocalError. "number" can't be a global name when used on
the right hand side, and a local name when used on the left hand
side. It's simple: it is local, or not, but not both.
why does a name of a function has something to do with a variable?Noticealso that it does not matter *what* kind of object a name
refers to: it may be a function, a class, an object instance,
whatever. Inside a function, by example, a local name "a" may be
bound at most to a single object at a time, it doesn't matter its
type. A local name "a" can't refer to an integer and a class at the same time.
The left hand side of an assign statement *always* uses local names,
except when you declare a name to be global by using the "global" keyword.
And notice also that I've never used the word 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?Yes, you can. Python has "lexically nested scopes". The simple
local/global rule of above is a bit more complicated: when a name is
not local, it is searched inside the enclosing functions, then in the
containing module's global namespace, and last in the builtin names.
And yes, b is local to a, so it effectively hides any external b that
could be in outer scopes.

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! -http://correo.yahoo.com.ar
Nov 11 '06 #8

P: n/a
Camellia wrote:
Oh how can I thank you enough, you make my day:)
According to what you said I finally figure it out, it is the same as:

<code>
b = 1
def a():
b = b #no good:)
</code>
if you really want to modify a variable that lives outside the function,
you can use the "global" directive:

http://effbot.org/pyfaq/how-do-you-s...a-function.htm
So in every day programming I should avoid using the same name for
different types of objects because they will step on each other,
right?
if you want to distinguish between things, giving them distinct names is
always a good idea.

</F>

Nov 11 '06 #9

P: n/a
Oh thank you for pointing that out Fredrik, you made the case more
clear:)

On Nov 11, 7:19 pm, Fredrik Lundh <fred...@pythonware.comwrote:
Camellia wrote:
Oh how can I thank you enough, you make my day:)
According to what you said I finally figure it out, it is the same as:
<code>
b = 1
def a():
b = b #no good:)
</code>if you really want to modify a variable that lives outside the function,
you can use the "global" directive:

http://effbot.org/pyfaq/how-do-you-s...e-in-a-functio...
So in every day programming I should avoid using the same name for
different types of objects because they will step on each other,
right?

if you want to distinguish between things, giving them distinct names is
always a good idea.

</F>
Nov 11 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.