473,396 Members | 1,864 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,396 software developers and data experts.

Scope

I want to write a function, foo, so the following works:

def main():
n = 4
foo(n)
print n

#it prints 7

if foo needs to take different arguments, that'd be alright.

Is this possible?

I already tried this (below), which doesn't work. foo only changes
the global n.
n = 3
def main():
def foo(var, context, c2):
exec var + " = 7" in context, c2

n = 4
foo("n", locals(), globals())
print n

if __name__ == '__main__': main()

print n
And of course I tried:
def inc(n): .... n += 3
.... a = 4
inc(a)
a

4

-- Elliot Temple
http://www.curi.us/
---
[This E-mail scanned for viruses by Declude Virus]

Jul 19 '05 #1
5 1266
Elliot Temple wrote:
I want to write a function, foo, so the following works:

def main():
n = 4
foo(n)
print n

#it prints 7


What's wrong with:

def foo(n):
return 7

def main():
n = 4
n = foo(n)
print n

Anything else (including the tricks involving mutable objects that will
no doubt be posted) will result in ugly, hard to maintain code.
Jul 19 '05 #2

On Jun 4, 2005, at 2:13 AM, Leif K-Brooks wrote:
Elliot Temple wrote:
I want to write a function, foo, so the following works:

def main():
n = 4
foo(n)
print n

#it prints 7


What's wrong with:

def foo(n):
return 7

def main():
n = 4
n = foo(n)
print n

Anything else (including the tricks involving mutable objects that
will
no doubt be posted) will result in ugly, hard to maintain code.


Nothing is wrong with it in this case. I just want to know if Python
can do what I said.

-- Elliot Temple
http://www.curi.us/
---
[This E-mail scanned for viruses by Declude Virus]

Jul 19 '05 #3
Elliot Temple wrote:
Nothing is wrong with it in this case. I just want to know if Python
can do what I said.


With a number of difficult hacks, yes. Passing around objects as
namespaces, however, is vastly easier and far superior.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #4
On Friday 03 June 2005 07:17 pm, Elliot Temple wrote:
Nothing is wrong with it in this case. I just want to know if Python
can do what I said.


Read the python-list "working with pointers" thread from Tuesday. Good answers
were posted there.

James

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

http://www.jamesstroud.com/
Jul 19 '05 #5
Elliot Temple wrote:
I want to write a function, foo, so the following works:

def main():
n = 4
foo(n)
print n

#it prints 7

if foo needs to take different arguments, that'd be alright.

Is this possible?


It is possible if you pass mutable objects to foo such as lists or
dictionaries.

Is this what you are looking for?

def main():
d = [3,]
foo(d)
print d[0]

def foo(var):
var[0] = 7

main()
Cheers,
_Ron

Jul 19 '05 #6

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

Similar topics

3
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers...
6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
3
by: marco_segurini | last post by:
Hi, I am using VS 2005. If I compile the following code only line 6 returns me an error while line 9 returns a warning. If I comment the line 6 and debug the program the assignments of lines...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
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; //...
1
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid,...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.