473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Scope question

Hi,

How can I make counter variable in function foo reference to global counter
variable in the following code. Generally C programmers tend to write code like
that I am looking for simple way to do it in python.

--- BEGIN ---
counter = 0

def foo():
if counter < 10:
print "count = ", counter
counter += 1

foo()
--- END ---

regards,
Subhash
--
Remove "nospam_" for direct reply
Jul 18 '05 #1
3 1301
Subhash Chandra wrote:
Hi,

How can I make counter variable in function foo reference to global
counter variable in the following code. Generally C programmers tend
to write code like that I am looking for simple way to do it in python.

--- BEGIN ---
counter = 0

def foo():
if counter < 10:
print "count = ", counter
counter += 1

foo()
--- END ---


You need to tell explicitly that the name 'counter' is global. It is
because used this statment in your function:

counter += 1

which is a rebind of the name 'counter'. So python thinks it must be a
local name.
Here is what you want:

counter = 0

def foo():
global counter
if counter < 10:
print "count = ", counter
counter += 1

foo()

Jul 18 '05 #2
Subhash Chandra <no**************@yahoo.com> writes:
How can I make counter variable in function foo reference to global
counter variable in the following code.
add the line " global counter" just after "def foo():"
Generally C programmers tend to write code like that I am looking
for simple way to do it in python.
Generally C programmers do horrible things. Please consider whether
you really, really should be emulating C style in Python.
--- BEGIN ---
counter = 0

def foo():
if counter < 10:
print "count = ", counter
counter += 1

foo()
--- END ---

Jul 18 '05 #3
Hello Subhash,
--- BEGIN ---
counter = 0

def foo():
if counter < 10:
print "count = ", counter
counter += 1

foo()
--- END ---

Today we have generators:
def foo(): for count in range(10):
print "count = ", count
yield count
count += 1 gen = foo()
gen.next() count = 0
0 gen.next() count = 1
1


HTH.
Miki
Jul 18 '05 #4

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

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
6
by: Razvan | last post by:
Hi, I took a C++ test and I found the following question: Q. Inside a class member function definition, which scope is searched
9
by: Vipul Jain | last post by:
Can any one please tell me what is the difference between global scope of an variable and file scope of an variable. Vipul
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
6
by: Frank Silvermann | last post by:
I have taken an extraordinary leap into the modern world by purchasing webspace. In addition to my private concerns, I would like to make a part to which others, e.g. my nieces and ex-wife, can...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
2
by: Michael | last post by:
Hello, I have a newbie question about class scope. I am writting a little program that will move files to one of two empty folders. I am having a hard time understanding scope. So this will be a...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.