Scope

E

Elliot Temple

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:
.... n += 3
....4

-- Elliot Temple
http://www.curi.us/
 
L

Leif K-Brooks

Elliot said:
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.
 
E

Elliot Temple

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/
 
R

Robert Kern

Elliot said:
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
(e-mail address removed)

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

James Stroud

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/
 
R

Ron Adam

Elliot said:
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
 
P

Peter Dembinski

Elliot Temple said:
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, but the more natural way would be to use function
return value:

n = f(n)

or, if you need many assignments:

a, b, c, d = f(a, b, c, d)

and, respectively:

return a, b, c, d

in the function's body.
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

You need to make 'n' globally visible. See the 'global' keyword
in Python user manual.
And of course I tried:

... n += 3
...
4

AFAIK inc is builtin function. And builtin functions doesn't have to
be real functions, they can be just aliases to Python's VM bytecodes
or sets of bytecodes.
 
S

Steven Bethard

Peter said:
AFAIK inc is builtin function. And builtin functions doesn't have to
be real functions, they can be just aliases to Python's VM bytecodes
or sets of bytecodes.

Wrong on both counts. ;)

py> inc
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'inc' is not defined
py> __builtins__.inc
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'module' object has no attribute 'inc'

There is no builtin 'inc'. To verify, check:
http://docs.python.org/lib/built-in-funcs.html
http://docs.python.org/lib/non-essential-built-in-funcs.html


And while builtin functions may not have to be real *functions*, they do
have to be real *callables*:

py> type(abs)
<type 'builtin_function_or_method'>
py> help(type(abs))
Help on class builtin_function_or_method in module __builtin__:

class builtin_function_or_method(object)
| Methods defined here:
|
| __call__(...)
| x.__call__(...) <==> x(...)
....
py> type(bool)
<type 'type'>
py> help(type(bool))
Help on class type in module __builtin__:

class type(object)
| type(object) -> the object's type
| type(name, bases, dict) -> a new type
|
| Methods defined here:
|
| __call__(...)
| x.__call__(...) <==> x(...)
....

Note that both <type 'builtin_function_or_method'> and <type 'type'>
have a __call__ method, which means they are callable objects. They're
not just bytecodes; they're real objects, just like everything else in
Python. =)

STeVe
 
P

Peter Dembinski

Steven Bethard said:
Wrong on both counts. ;)

Yup. My mistake.

[snap]
And while builtin functions may not have to be real *functions*,
they do have to be real *callables*:

Yes. I discovered it yesterday.

[snap]
Note that both <type 'builtin_function_or_method'> and <type 'type'>
have a __call__ method, which means they are callable objects.
They're not just bytecodes; they're real objects, just like
everything else in Python. =)

Yup. True.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top