473,398 Members | 2,113 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,398 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 1304
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.