scope of variables

G

Gary Wessle

Hi

is the code below correct?

b = 3
def adding(a)
print a + b

it seams not to see the up-level scope where b is defined.

thanks
 
M

Marc 'BlackJack' Rintsch

is the code below correct?
No...

b = 3
def adding(a)

....a colon is missing at the end of the above line.
print a + b

it seams not to see the up-level scope where b is defined.

It does. And you could easily find out yourself by just trying that code.
Running this::

b = 3
def adding(a):
print a + b

adding(5)

Puts out 8 as expected.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steve R. Hastings

b = 3
def adding(a)
print a + b

it seams not to see the up-level scope where b is defined.

Assuming you put a ':' after the "def adding(a)", this should work in
recent versions of Python. In Python 2.0 and older, this will not work.


In Python 2.1, it will only work if you do this:

from __future__ import nested_scopes


When you first start Python interactively, it should print version
information. Here's what my Python prints when I start it:

Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.


As you can see, I'm running Python 2.4.3. Make sure you aren't running an
old version of Python, and that code should do what you expect.
 
B

Ben Finney

Gary Wessle said:
is the code below correct?

It's best to post an example that you've tried yourself, and that is
small but completely demonstrates the issue in question.
b = 3
def adding(a)
print a + b

This, for example, would fail the syntax check (the 'def' statement
needs a trailing colon). After that's fixed, the code runs, but shows
no output.

A complete, minimal, working example will help people answer your
question.
 
G

Gary Wessle

Steve R. Hastings said:
Assuming you put a ':' after the "def adding(a)", this should work in
recent versions of Python. In Python 2.0 and older, this will not work.

the example was an in-accuretlly representation of a the problem I am
having. my apologies.

a = []
def prnt():
print len(a)
<function prnt at 0xb7dc21b4>

I expect to get 0 "the length of list a"
 
L

Lord Landon

Try >>> prnt()
o.o'

Steve R. Hastings said:
Assuming you put a ':' after the "def adding(a)", this should work in
recent versions of Python. In Python 2.0 and older, this will not work..

the example was an in-accuretlly representation of a the problem I am
having. my apologies.

a = []
def prnt():
print len(a)
<function prnt at 0xb7dc21b4>

I expect to get 0 "the length of list a"
 
R

Rob E

is the code below correct?
b = 3
def adding(a)
print a + b

it seams not to see the up-level scope where b is defined.

Yes except for the missing : at the end of the "def" line.

Rob
 
G

Gary Wessle

Ryan Forsythe said:
Gary said:
the example was an in-accuretlly representation of a the problem I am
having. my apologies.

a = []
def prnt():
print len(a)
<function prnt at 0xb7dc21b4>

I expect to get 0 "the length of list a"

You want prnt(), not prnt:

I finally was able to duplicate the error with a through away code
as follows,

****************************************************************
acc = [1,2,3]

def a():
b = [4, 5, 6]
acc = acc + b
print len(acc)

a()

**************** error ****************
Traceback (most recent call last):
File "a.py", line 12, in ?
a()
File "a.py", line 9, in a
acc = acc + b
UnboundLocalError: local variable 'acc' referenced before assignment
 
B

bruno at modulix

Gary said:
(snip)
I finally was able to duplicate the error with a through away code
as follows,

****************************************************************
acc = [1,2,3]

def a():
b = [4, 5, 6]
acc = acc + b
print len(acc)

a()

**************** error ****************
Traceback (most recent call last):
File "a.py", line 12, in ?
a()
File "a.py", line 9, in a
acc = acc + b
UnboundLocalError: local variable 'acc' referenced before assignment

This is a FAQ:
http://www.python.org/doc/faq/progr...ules-for-local-and-global-variables-in-python

For short: if a name is 'assigned' (in Python, the correct term is
'bound') in the local scope, it'll be considered a local name. If it's
*only* accessed, it'll be looked up in the enclosing namespace - here
the so-called 'global' (which means: 'module') namespace.

The dirty solution is to declare 'acc' as global in the function:
def a():
b = [4, 5, 6]
global acc
acc = acc + b
print len(acc)

but this is really BadCode(tm). As a general rule, functions should not
silently modify or rebind global variables - this leads to maintenance
nightmares. In this case, you should manage to either 1/ pass acc as a
param to a(), or 2/ have a() return the sequence to be added to acc:

# 1
acc = [1,2,3]
def a(alist):
alist.extend([4, 5, 6])
return alist

acc = a(acc)
print acc, len(acc)

# 2
acc = [1,2,3]
def a():
return [4, 5, 6]

acc.extend(a())
print acc, len(acc)


The Right Thing(tm) to do of course depends on the real code, so it may
be yet another solution, but it's impossible to decide with dummy code...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top