[Tutor] pointers for python?
Gonçalo Rodrigues
op73418 at mail.telepac.pt
Fri Oct 3 21:28:01 EDT 2003
On Sat, 4 Oct 2003 12:10:01 +1200, you wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>
>Hi all,
>
>In C, you can modify a variable from within a seperate function, by passing a
>pointer to that function. Is there any way to do this in python? it'd be
>kinda useful ;)
>
>I'm sure i read it in the tutorial a while back, but searching the same
>tutorials now, I can't seem to find it anywhere... any ideas?
>
>to clarify, currently, this:
>
>def inc(x):
> x+=1
>
>a=1
>inc(a)
>print a
>
>prints "1"
>
>is there a way to get the inc() function to modify the original variable, even
>though it's not in the function's scope?
>
Why not the perfectly Pythonic solution
>>> def inc(x):
... return x + 1
...
>>> a = 1
>>> a = inc(a)
>>> a
2
It even has no side effects - the function that is.
With my best regards,
G. Rodrigues
More information about the Tutor
mailing list