472,334 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

scopes of local and global variable

#############################CODE################# #############
t_len=0
class WORK:
def getwork(self):
def formattable(table_to_process,type):
TYPE=["p","t","T","s","i"] #list of types to format
if type==TYPE[1]:
def format_t():
row=[]
for col in table_to_process:

#######################
# ERROR PRONE PART #
#######################
if len(str(col))>t_len:
t_len=len(str(col))
#######################
# Error message says: #
# UnboundLocalError: local variable 't_len' referenced before assignment#

row+=col
if (table_to_process.index(col)+1)%7==0:
t_temp.append(row)
row=[]
format_t()
################################################## ###############

Interpreter says that t_len is local variable although i have
specified t_len=0 in line 1. Also, although i've stated t_len=0 in
line 1, it says that t_len is referenced before assignment.
Dec 22 '06 #1
9 1619
Pyenos wrote:
#############################CODE################# #############
t_len=0
class WORK:
def getwork(self):
def formattable(table_to_process,type):
TYPE=["p","t","T","s","i"] #list of types to format
if type==TYPE[1]:
def format_t():
row=[]
for col in table_to_process:

#######################
# ERROR PRONE PART #
#######################
if len(str(col))>t_len:
t_len=len(str(col))
#######################
# Error message says: #
# UnboundLocalError: local variable 't_len' referenced before assignment#

row+=col
if (table_to_process.index(col)+1)%7==0:
t_temp.append(row)
row=[]
format_t()
################################################## ###############
wow.
Interpreter says that t_len is local variable although i have
specified t_len=0 in line 1. Also, although i've stated t_len=0 in
line 1, it says that t_len is referenced before assignment.
each function introduces a new scope.

</f>

Dec 23 '06 #2
Fredrik Lundh <fr*****@pythonware.comwrites:
Pyenos wrote:
#############################CODE################# #############
t_len=0
class WORK:
def getwork(self):
def formattable(table_to_process,type):
TYPE=["p","t","T","s","i"] #list of types to format
if type==TYPE[1]:
def format_t():
row=[]
for col in table_to_process:
#######################
# ERROR PRONE PART #
#######################
if len(str(col))>t_len:
t_len=len(str(col))
#######################
# Error message says: #
# UnboundLocalError: local variable 't_len' referenced before assignment#
row+=col
if (table_to_process.index(col)+1)%7==0:
t_temp.append(row)
row=[]
format_t()
################################################## ###############

wow.
Interpreter says that t_len is local variable although i have
specified t_len=0 in line 1. Also, although i've stated t_len=0 in
line 1, it says that t_len is referenced before assignment.

each function introduces a new scope.

</f>
does class WORK inherit t_len=0 from line1?

does def getwork() inherit t_len=0 from line1?

does def formattable(table_to_process,type) inherit t_len=0 from line1?

does def format_t() inherit t_len=0 from line1?

thanks.

Dec 23 '06 #3
Pyenos wrote:
Fredrik Lundh <fr*****@pythonware.comwrites:

>>Pyenos wrote:

>>>#############################CODE############## ################
t_len=0
class WORK:
def getwork(self):
def formattable(table_to_process,type):
TYPE=["p","t","T","s","i"] #list of types to format
if type==TYPE[1]:
def format_t():
row=[]
for col in table_to_process:
#######################
# ERROR PRONE PART #
#######################
if len(str(col))>t_len:
t_len=len(str(col))
#######################
# Error message says: #
# UnboundLocalError: local variable 't_len' referenced before assignment#
row+=col
if (table_to_process.index(col)+1)%7==0:
t_temp.append(row)
row=[]
format_t()
############################################### ##################

wow.

>>>Interpreter says that t_len is local variable although i have
specified t_len=0 in line 1. Also, although i've stated t_len=0 in
line 1, it says that t_len is referenced before assignment.

each function introduces a new scope.

</f>


does class WORK inherit t_len=0 from line1?

does def getwork() inherit t_len=0 from line1?

does def formattable(table_to_process,type) inherit t_len=0 from line1?

does def format_t() inherit t_len=0 from line1?

thanks.
Yes and no, depending.

The rule of thumb I use is that if you assign in a function, the name is
not taken from the enclosing scope. For example, compare:

# example 1
def doit():
t_len = 42
def nested():
print t_len
nested()

doit() # will print 42
# example 2
def doit():
t_len = 42
def nested():
if t_len 0:
print t_len
else:
t_len = 1
nested()

doit() # will get "referenced before assignment" error
You could make use of your WORK class here, depending on whether t_len
makes sense as a member of the WORK class:
class WORK:
t_len = 0
def getwork(self):
def formattable(table_to_process,type):
# etc., etc.
WORK.t_len = len(str(col))

James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Dec 23 '06 #4
James Stroud <js*****@mbi.ucla.eduwrites:
>>#############################CODE############### ###############
t_len=0
class WORK:
def getwork(self):
def formattable(table_to_process,type):
TYPE=["p","t","T","s","i"] #list of types to format
if type==TYPE[1]:
def format_t():
row=[]
for col in table_to_process:
#######################
# ERROR PRONE PART #
#######################
if len(str(col))>t_len:
t_len=len(str(col))
#######################
# Error message says: #
# UnboundLocalError: local variable 't_len' referenced before assignment#
row+=col
if (table_to_process.index(col)+1)%7==0:
t_temp.append(row)
row=[]
format_t()
################################################ #################
based on your advice i will try to answer my own questions:
does class WORK inherit t_len=0 from line1?
yes.
does def getwork() inherit t_len=0 from line1?
no.
does def formattable(table_to_process,type) inherit t_len=0 from
line1?
no.
does def format_t() inherit t_len=0 from line1?
no.

thank you kindly.
Dec 23 '06 #5
Pyenos wrote:
does class WORK inherit t_len=0 from line1?

does def getwork() inherit t_len=0 from line1?

does def formattable(table_to_process,type) inherit t_len=0 from line1?

does def format_t() inherit t_len=0 from line1?
Not really, no. The global t_len is different than the local t_len--two
variables with the same name. You need to declare "global t_len" inside
your function so it knows that "t_len=..." is assigning to the old,
global variable instead of creating a new one.

See #6 here: http://zephyrfalcon.org/labs/python_pitfalls.html

-Max

Dec 23 '06 #6
"Max Wilson" <wi********@gmail.comwrites:
Pyenos wrote:
does class WORK inherit t_len=0 from line1?

does def getwork() inherit t_len=0 from line1?

does def formattable(table_to_process,type) inherit t_len=0 from line1?

does def format_t() inherit t_len=0 from line1?

Not really, no. The global t_len is different than the local t_len--two
variables with the same name. You need to declare "global t_len" inside
your function so it knows that "t_len=..." is assigning to the old,
global variable instead of creating a new one.

See #6 here: http://zephyrfalcon.org/labs/python_pitfalls.html

-Max
so, based on your advice, i think the answers are all no.
Dec 23 '06 #7
At Friday 22/12/2006 20:45, Pyenos wrote:
># Error message says: #
# UnboundLocalError: local variable 't_len' referenced before assignment#
See item 6 in "10 Python pitfalls":
<http://zephyrfalcon.org/labs/python_pitfalls.html>
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Dec 23 '06 #8
Pyenos wrote:
does class WORK inherit t_len=0 from line1?

does def getwork() inherit t_len=0 from line1?

does def formattable(table_to_process,type) inherit t_len=0 from line1?

does def format_t() inherit t_len=0 from line1?
you may want to read the documentation:

http://docs.python.org/ref/naming.html

</F>

Dec 23 '06 #9
Pyenos wrote:
does class WORK inherit t_len=0 from line1?

does def getwork() inherit t_len=0 from line1?

does def formattable(table_to_process,type) inherit t_len=0 from line1?

does def format_t() inherit t_len=0 from line1?
you may want to read the documentation:

http://docs.python.org/ref/naming.html

</F>

Dec 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Miki Tebeka | last post by:
Hello, Can anyone explain why: >>> def make_inc(n): s = n def inc(i): s += i return s return inc
2
by: Fernando Rodriguez | last post by:
Hi, I have a parameter defined in a module, called PREVIEW. Many functions use it's value to modify their behavior. A function called...
3
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
5
by: Dave Benjamin | last post by:
I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): >>> def f(x): ... ...
4
by: kj | last post by:
I'm a Perlhead (there, I said it). Years ago I made a genuine attempt to learn Python, but my intense disappointed with the way Python deals with...
46
by: Albert | last post by:
Why doesn't: #include <stdio.h> void reverse(char, int); main() { char s;
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is,...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created...
3
by: giblfiz | last post by:
I was hoping that someone on the group might have an idea of how to access scopes (or symbol tables) other than the one you are currently running...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.